mercredi 20 janvier 2016

laravel5.1 retrieve nested relation using eloquent hasOne() (one to one relation)

My table design is:

users: | id  |username | ... |

tickets: | id | supp_id | ... |

ticket_replies: | id | ticket_id | user_id |

My controllers look like:

user:

public function tickets()
{
    return $this->hasMany('App\ticket');
}

ticket:

public function ticket_replie()
{
    return $this->hasMany('App\ticket_replie');
}

public function supporter()
{
    return $this->hasOne('App\User', 'id','supp_id');
}

My controller looks like this:

$tickets =  Auth::user()->tickets()->orderBy('status', 'desc')->paginate(2);

return view('protected.ticketsList', [ 
    'tickets' => $tickets,
]);

In my view I use:

 supporter: {{ $ticket['supporter']->username }}

Any idea where I do wrong? I get all the time this error:

Trying to get property of non-object

In my point of view the relation between the tables itself is done correctly. When I use for example {{ dd($ticket) }} in my view I get an object with all the items I need:

image



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire