Connection Model :
protected $table = 'connections';
protected $fillable = ['user_id_1','user_id_2','connection_status'];
public function user_id_1()
{
return $this->belongsTo('App\User', 'user_id_1');
}
public function user_id_2()
{
return $this->belongsTo('App\User', 'user_id_2');
}
Controller :
public function show($id){
$user_id_1_connections = Connection::whereUser_id_1AndConnection_status($id, 1)->get();
$user_id_2_connections = Connection::whereUser_id_2AndConnection_status($id, 1)->get();
return view('connection.showConnection',['user_id_1_connections' => $user_id_1_connections, 'user_id_2_connections' => $user_id_2_connections]);
}
Blade:
@foreach($user_id_1_connections as $user_id_1_connection)
{{ $user_id_1_connection->user_id_1 ? $user_id_1_connection->user_id_1->name : 'unknown' }}
@endforeach
@foreach($user_id_2_connections as $user_id_2_connection)
{{ $user_id_2_connection->user_id_2 ? $user_id_2_connection->user_id_2->name : 'unknown' }}
@endforeach
I am making two relationships to user in my model Connection.php. I'm getting the error: "Trying to get property of non-object " in line 2nd and 5th in blade.
First change:
into:
to avoid collisions between table columns and relationships
Instead of this:
you can do:
And now your template could look like this:
But to be honest it's hard to get what you really want to achieve - you are using strange function
whereUser_id_1AndConnection_status
what you should rather don't do so probably you should read carefully relationships documentation for Laravel.