Reputation: 530
tables
persons:
id = 1
name = 'john'
class_of_pesson:
id = 1,
desc = 'employee'
classes_of_person:
persons_id = 1
class_of_pesson_id = 1
Persons::with('classes_of_person.class_of_pesson')->paginate(10);
data:
[
{ id: 1, name: 'jonh', classes_of_person: [ {person_id: 1, class_id: 1, class_of_pesson: { id: 1, desc: 'employee' } } ] }
]
wanted:
[
{ id: 1, name: 'jonh', classes_of_person: [{id: 1, desc: 'employee'}] }
]
I tried the pluck method but I am confused. Can anyone help me with this?
Edit:
Models Relationship
Persons
public function classes_of_person() {
return $this->hasMany(classes_of_person::class, 'person_id', 'id');
}
ClassesOfPersons
public function classe_of_person() {
return $this->hasOne(classe_of_pesson::class, 'id', 'class_of_person_id')
}
Solved with belongsToMany in person model. Thanks for all
Ex: function classes_of_person () {
return $this->belongsToMany(classe_of_pesson::class);
}
Upvotes: 1
Views: 793
Reputation: 131
What you need is to define a HasManyThrough relation in your model then use it in your query with the "with" function.
Check the reference: https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
Upvotes: 1