Left Join Laravel Eloquent ORM

  eloquent, laravel

I’m trying to go a little deeper into Eloquent ORM relationships and can’t get my head around doing this:

$a = new Attachments();
$tmp = $a->where( 'attached_to', ATTACHMENTS::ATTACHED_TO_TASK )
           ->where( 'db_id', $task_id )
           ->select( 'db_id', 'user_id', 'attachments.id', 'real_filename', DB::raw( "concat(first_name,' ',last_name) as uploader" ) )
           ->leftJoin( 'users', 'users.id', '=', 'user_id' )
           ->get();

I’ve got this far:

$tmp=Tasks::find($task_id)->attachments->where('attached_to',ATTACHMENTS::ATTACHED_TO_TASK);

… and I can live without the concat, but I can’t figure out for the life of me the syntax to get details from the users table in the same statement.

Tasks have many attachments. Attachments from many users belong to one task. Attachments belong to one user.

Source: Laravel

Leave a Reply