These are my models:
class Branch extends Eloquent {
protected $table = 'tb_branch';
public function faculty()
{
return $this->hasMany('Faculty');
}
}
class Faculty extends Eloquent {
protected $table = 'tb_faculty';
public function branch()
{
return $this->belongsTo('Branch');
}
}
In branch table I have set a column with foreign key user_id that links to users table.
In faculty table I have set a column with foreign key branch which links to branch table with column name.
And now I need to retrieve Faculty data based on currently logged in user (with Eloquent).
I imagine the work flow would be:
- retrieve id of currently logged in user
- link that id to the branch's
user_idand retrieve branch'sname - link that branch's
nameto faculty branch'snameand retrieve all matched faculty data
Now this is what I have tried, it didn't work:
$user_id = Sentry::getUser();
$faculty = Faculty::with('branch')->where('branch.user_id',$user->id)->get();
Error says:
Unknown column 'branch.user_id' in 'where clause' (SQL: select * from
tb_facultywherebranch.user_id= 6)
How can I achieve this with Eloquent?