1

I have four tables and I am giving my table structure here

  1. user_work['id', 'user_id', 'work_id']
  2. work_sectors['id', 'name', 'status']
  3. works['id', 'work_sector_id', 'work_type_id', 'work_duration_id', 'name']
  4. users['id', ...]

And My Models are

class User extends Eloquent implements UserInterface, RemindableInterface 
{
  use UserTrait, RemindableTrait;
  protected $table = 'users';
  public function work()
  {
       return $this->belongsToMany('Work', 'user_work');
  }
}

class Work extends \Eloquent {
protected $fillable = [];
protected $table_name = 'works';
public $timestamps = false;

public function user()
{
    return $this->belongsToMany('User', 'user_work');
}

public function sector()
{
    return $this->belongsTo('WorkSector', 'work_sector_id');
}
}

In my controller I have written this code

$user = User::with('language')->with('work')->find($userId);

Here I need name of work_sector table but probably I have written wrong code to get the sector name.

So please help me to write a proper function in this eloquent method in laravel 4.2.

Dixit Sourav
  • 350
  • 1
  • 3
  • 10
  • Looks like the same question: http://stackoverflow.com/questions/22868362/laravel-4-1-eager-loading-nested-relationships-with-constraints – Anatoliy Arkhipov Mar 21 '15 at 12:11
  • You can't get just one name from `work_sector` because your user can possibly have multiple works (and therefore sectors) assigned – lukasgeiter Mar 21 '15 at 12:13

0 Answers0