mercredi 20 janvier 2016

laravel 5.1 eloquent hasMany relation retrieve values

My table design is:

users: |id|username|...

tickets: |id|title|status|...

ticket_replies: |id|ticket_id|text...

My controllers look like:

user:

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

ticket:

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

My model looks like this:

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

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

In my view I use:

div class="container">
@if(!$tickets->count() > 0)
no tickets created
@endif
@foreach ($tickets as $ticket)
{{dd($ticket->ticket_replie)}}
    title: {{ $ticket->title}}<br>
    status: {{ returnStatus($ticket->status) }}<br>
    text: {{  $ticket->ticket_replie->text }}<br>
    created: {{ $ticket->created_at }}<br>
@endforeach

The line

{{dd($ticket->ticket_replie)}}

works fine, I get a collection with all relevant information of the found ticket_replies. But how can I access for example the "text" field in this collection? All I tried doesn't work, I tried:

text: {{  $ticket->ticket_replie->text }}<br>

Any idea where I do wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire