Answer the question
In order to leave comments, you need to log in
How to access related object fields in Laravel?
The project has two models: Department and Person. These models are defined with a many-to-many relationship. that is, each Person can own multiple Departments, and each Department can own multiple Persons.
For person:
class Person extends Model
{
protected $table = 'persons';
public function departments (){
return $this->belongsToMany('App\Department')->withTimestamps();
}
}
class Department extends Model
{
public function persons(){
return $this->belongsToMany('App\Persons')->withTimestamps();
}
}
$person = App\Person::find(10);
$person->departments()->attach(1,5);
$person->toArray();
return $person->departments;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question