Laravel 5 trouble using Eager Loading -


so have problem getting user data through staff table using eager loading.

controller:

public function staff() {     $salon = salon::find(session::get('salon'));     $staff = staff::where('salon', $salon->id)->with('user')->get();      return view('dashboard.salon.staff', compact('staff')); } 

view:

@foreach ($staff $staff)     {{ $staff->user->photo }} @endforeach 

model:

class staff extends model {      protected $table = 'salonstaff';      public function user()     {         return $this->belongsto('app\user');     } } 

i have tried troubleshooting using dd($staff) , user relation appears null controller, , doing dd($staff->user) inside foreach results in "1", nothing else.

if primary key in user column = 'user', should specify in user model with

protected $primarykey = 'user'; 

if not, when using relation, eloquent expects conventional foreign key 'user_id' ($tablename_id)

just sure, try specify foreign key in model relation:

return $this->belongsto('app\user', 'user'); 

where second argument real foreign key in table.

also try change query this

$staff = staff::where('salon', $salon->id)->user()->get(); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -