1

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);
}
1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

could you show me an example using my models please?
Solved with belongsToMany in person model laraveldaily.com/pivot-tables-and-many-to-many-relationships

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.