peke_peke
peke_peke

Reputation: 551

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

Upvotes: 1

Views: 646

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29314

So first of all, trying to access the function public function supporter() on your Ticket model cannot be done using array syntax. Change:

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

to:

{{ $ticket->supporter()->first()->username }}

If you don't want to use the ->first() you can do one of two things. Modify your existing query to include the ->with("supporter") syntax:

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

And access the supporter from the view like so:

{{ $ticket->supporter->username }}
// Notice how supporter is no longer a `method` ->supporter() but a `property` ->supporter

Or you can modify your relationship to include the closer ->first():

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

And then access it like so:

{{ $ticket->supporter()->username }}
// Notice you no longer need `->first()`

So, as you can see, there are multiple ways to access a Ticket's Supporter. Please note that you can't really combine these options; modifying the function to include ->first() and then trying to use ->with() will return an error, so pick one or the other.

Hope that helps!

Upvotes: 1

Related Questions