jeudi 24 mai 2018

Retrieve custom fields from child of a child model in laravel eloquent

I'm trying to retrieve custom fields from a model using the with() statement in laravel. When i just get all fields the child object comes ok, but when i try to retrieve just some fields of this child objetct it returns to me the following error: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous, below i'll show how i'm doing these queries.

When i execute the following query it works:

 $cfps = CallForPaper::select('id', 'speech_id')->with([
            'speech:id,title,description',
            'speech.speakers'
         ])->get();

But when i execute the code below it returns the error wich i have mentioned above.

$cfps = CallForPaper::select('id', 'speech_id')->with([
            'speech:id,title,description',
            'speech.speakers:id,name,email'
         ])->get();

Note: than only modification in this code is the line: 'speech.speakers:id,name,email'

in my model Speech, the speakers relationship is the following:

public function speakers()
    {
        return $this->belongsToMany(Speaker::class, 'speech_speaker');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire