1

Good morning,

I've been trying for quite a lot of time to translate this query(which returns an array of stdClass) into query builder so I could get objects back as Eloquent models.

This is how the query looks like untranslated:

$anketa = DB::select( DB::raw("SELECT * 
                                         FROM v_anketa a 
                                        WHERE not exists (select 1 from user_poeni where anketa_id=a.id and user_id = :lv_id_user)
                                        Order by redni_broj limit 1"
                                     ), array(   'lv_id_user' => $id_user,
                ));

I have tried this, but it gives a syntax error near the inner from in the subquery:

$anketa = V_anketa::selectRaw("WHERE not exists (select 1 from user_poeni where anketa_id=a.id and user_id = :lv_id_user)", array('lv_id_user' => $id_user,)
                            )->orderBy('redni_broj')->take(1)->first();

The problem is this exists and a subquery in it. I couldn't find anything regarding this special case.

Assume each table has an appropriate Eloquent model. V_anketa is a view. The db is postgresql.

2 Answers 2

4

As far as the query goes I believe this should work:

$anketa = V_anketa::whereNotExists(function ($query) use ($id_user) {
                $query->select(DB::raw(1))
                      ->from('user_poeni')
                      ->where('anketa.id', '=', 'a.id')
                      ->where('user_id', '=', $id_user);
            })
           ->orderBy('redni_broj')
           ->first();

but I'm not clear on what do you mean by "assuming every table has an Eloquent model" and "V_anketa" is a view...

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I needed. I have just did it myself, I will accept your answer.
Thats just added information. For example the table user_poeni has an Eloquent model name UserPoeni etc. I added the information about V_anketa since it itself points to a view not to a table. I'm still quite new to Laravel so I assumed there could be some difference there.
1

Assuming the SQL query is correct, this should work:

$anketa = DB::select(sprintf('SELECT * FROM v_anketa a WHERE NOT EXISTS (SELECT 1 FROM user_poeni WHERE anketa_id = a.id AND user_id = %s) ORDER  BY redni_broj LIMIT 1', $id_user));

If you want to get back an Builder instance you need to specify the table:

$anketa = DB::table('')->select('');

If you however, want to get an Eloquent Model instance, for example to use relations, you need to use Eloquent.

3 Comments

This still returns an array of stdClass objects, not eloquent models.
What do you mean? This won't return "eloquent models" (I assume you mean eloquent instances) as you'r enot using Eloquent, but the query builder?
I probably haven't phrased the question right, my mistake. I want something like my try, i.e V_anketa::something which will give me results of type V_anketa and not an array of stdClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.