mercredi 13 juin 2018

Switching from Limit() to First() in Laravel Model

I'm stuck on an issue where I'm trying to clean up code presented to me, and I feel like I must've just been at this too long and can't see this issue.

At the moment I have the following code in one of my models:

public function attachments(){
    return $this->hasMany(volunteerAttachment::class, 'volunteerID','id');
}

 public function photoID() {
    $photoID= $this->attachments()->where('category','Drivers License')->orderBy('endDate','desc')->limit(1);
    return $photoID;
}

What I would like to do is what I believed was relatively simple in replacing the limit(1) of the function photoIdentification with a simple first().

But when I try that and simply put in my blade, it simply returns the error App\volunteer::photoID must return a relationship instance.

I do however know that there is a relationship because if I continue to use the limit(1) and put:

@foreach($volunteer->photoID as $id) 
   
@endforeach

It returns the relation and document correctly.

$volunteer is the variable for that particular model App\volunteer, and the following is how it is defined in the controller:

    public function show(Volunteer $volunteer){
            return view('volunteer.show', compact('volunteer'));
    } 

I'd appreciate any help. Thanks



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

Aucun commentaire:

Enregistrer un commentaire