Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zerodps's avatar

SQL Query with Variable / Laravel 5.6 MacOS

Hi all,

Im trying to amend my SQL Query which is working fine to use an variable (or a helper function).

$aa = DB::select('select projects.*, users.*, thumbnails.* from thumbnails inner join projects, users where projects.id = thumbnails.id && users.id_user = projects.user_id');

I would like to use this instead of users.id_user = projects.user_id

$this_user = auth::user()->id_user;

users.id_user = $this_user

Im not really comfortable using Eloquent it is one of my working progresses :)

Thanks for your help !

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Hey @zerodps fyi you are not using Eloquent but the QueryBuilder atm. So you can change what you need to bind with a question mark ? and then pass the value as second parameter:

DB::select('select projects.*, users.*, thumbnails.* from thumbnails inner join projects, users where projects.id = thumbnails.id && users.id_user = ?', [$this_user]);

To do it more of an Eloquent way you should have models created and the relationships setup within, more on this in the documentation.

1 like
zerodps's avatar

Hey @nakov,

thank you very much for your insight, as i mentioned the ORM topic is not my strongest topic :) I really need to take a closer look at the documentation !

Please or to participate in this conversation.