mercredi 21 décembre 2016

Join tables and getting specific fields Laravel 5

Objective

Get this data and combine into 1 collection/array:

//this three fields belongs to User table
id,
name,
last_name,
image_name //this field belongs to Image table

Tables

User Table:

id, name, last_name, gender, birthday, etc

Image Table id, user_id, image_name, image_comment, etc

Controller

$users = User::where(DB::raw("concat('name', ' ', 'last_name')"), 'LIKE', "%j%")
        ->orWhere('email', 'LIKE', "%j%")
        ->leftJoin('images', 'users.id', '=', 'images.user_id')
        ->take(6)
        ->get()
        ;

DD Result

i only get the users have record on image table

since letter j is the wild card. it suppose to get 6 data in users but since i used join i only get 2 users and join with image table. how can i get the 4 users data even without data in image table?

i use left join and i get all the data but some info mess up like users.id is equal to null



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire