Let me give you an example of laravel relationships

If User has many tasks and want to list in page Than in the task model define relation with user table and define tasks relationship in User model like below

class Task extends Eloquent {
public function user()
return $this->belongsTo(‘User’);

class User extends Eloquent {
public function tasks()
return $this->hasMany(‘Task’);

And, with that, we’re done! Let’s grab all tasks for the user with an id of 1.

$user = User::find(1);
$tasks = $user->tasks;


However, because we’ve defined the relationship from both ends, if we instead want to fetch the user associated with a task,

$task = Task::find(1);
$user = $task->user;
That’s how you can use them in model




1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)