Reputation: 551
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 supp_id()
{
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:
@foreach ($tickets as $ticket)
<br>
<br>
ID: {{$ticket->id}}
title: {{ $ticket->title}}<br>
status: {{ returnStatus($ticket->status) }}<br>
@if($ticket->supporter = null)
supporter: -<br>
@else
supporter: {{ $ticket->supp_id()->username }}
@endif
created: {{ $ticket->created_at }}<br>
@endforeach
Any idea where I do wrong? I get all the time this error:
Trying to get property of non-object (View: C:\xampp\htdocs\projekt\resources\views\protected\ticketsList.blade.php)
In my point of view the relation between the tables itself is done correctly.
Upvotes: 2
Views: 63
Reputation: 91
For starters I think your view should be like this so you only loop through the tickets if there are some.
@if(!$tickets->count() > 0)
no tickets created
@else
@foreach ($tickets as $ticket)
{{dd($ticket->ticket_replie)}}
title: {{ $ticket->title}}<br>
status: {{ returnStatus($ticket->status) }}<br>
supporter: {{ $ticket->supp_id->username }}
created: {{ $ticket->created_at }}<br>
@endforeach
@endif
Upvotes: 0
Reputation: 67525
The problem is in the following line :
supporter: {{ $ticket->supp_id->username }}
You're trying to get property username
from attribute (non-object) supp_id
, try to use parentheses ()
to refer to the method supp_id()
and not attribute supp_id
:
supporter: {{ $ticket->supp_id()->username }}
Note : You should put the foreach
loop inside @else
clause.
Hope this helps.
Upvotes: 1