vendredi 9 février 2018

Laravel identify collection instance key

I've setup a one to many relationships and I would like to retrieve the many results and display those onto my blade template. I'm currently getting the following error Property [job_title] does not exist on this collection instance.

I think this is because I'm trying to get the many results which although there is only one its a collection. My question is how do I know what the key is to loop around to echo my data e.g @foreach ($WhatHere? as something)

Here is my code -

Relationships

//Client model
public function contact() {
   return $this->hasMany(Contact::class, 'client_id', 'id');
}

//Contct model
public function client() {
    return $this->hasOne(Client::class, 'id', 'client_id');
}

Controller Query

public function show(Client $client, $id)
{
    $client = Client::where('id', $id)->firstOrFail();

    return view('clients.single', compact('client'));
}

Blade





via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire