NyseX NyseX - 2 months ago 5x
PHP Question

Laravel Getting User From Post

I have 3 tables that are related to each other.

Users, Clients, Notes

Users can add notes to a client.

The notes table has the following

id, user_id, client_id, note

On my clients page "client/{id}" I am printing all the notes that have been added for the particular client.

However, I am unable to get the name of the user from the Users table from the note.

It says Trying to get property of non-object when i try to pull the name of the User who posted the note.


$client = Client::find($cid);

if (empty($client)) {
return Redirect::to('contact'); //redirect to contacts if contact not found
} else {
$this->layout->content = View::make('clients.show')->with('client', $client);

Client view

@foreach ($client->notes as $note)
{{ $note->note }} //This works just fine.
Posted by: {{ $note->user->name }}

My Note model:

public function user() {
return $this->belongsTo('User','user_id');

My User model:

public function notes() {
return $this->hasMany('Note','user_id');

If I try and use the function I can do something like


But this will print out the entire array (it is the correct array) of the user row.


Try: Change this line

$client = Client::find($cid);


$client = Client::find($cid)->load('notes.user');


$client = Client::where('id',$cid)->with('notes.user')->get();