samedi 19 octobre 2019

I am trying to eager load parameter in eloquent laravel 5.8

I am trying to eager load data from a many-to-many relationship in eloquent laravel 5.8. The below code was working in laravel 5.5 but now it is not working in my new version i.e. 5.8

 $data = $this->contextObj->findData($id);

        $candidates = $data->candidates()
            ->with([
                'status'=> function ($query) use ($id)
                {
                    return  $query->where('recruitment_id', $id);
                }
            ])
            ->get();

My relation is as follows:

In Recruitment.php

public function candidates()
    {
        return $this->belongsToMany(Candidate::class)->withPivot('status');
    }

In Candidate.php

 public function status()
    {
        return $this->belongsToMany(Recruitment::class, 'candidate_recruitment')
                    ->select(['candidate_id','status']);
    }

Result in dump:

 #relations: array:1 [
        "status" => MediableCollection {
          #items: []
        }

is giving me empty results. Am I missing something needed in the new version. Thanks in advance for your help and sorry for my English



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/35QKIEQ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire