Sorry, I couldn't find a better title for this question.
I want to show "follow" or "Edit Profile" link, depends on whether authenticated user is watching his own profile or other user's profile.
Here is my blade code:
@if(isLogedIn())
@if($authedUser->id !== $profile->user()->find(1)->id)
{{link_to_action('RelationshipsController@add', 'Follow', $profile->user()->find(1)->id, ['class' => 'button radius'])}}
@else
{{link_to_action('ProfilesController@edit', 'Edit Profile', $authedUser->id, ['class' => 'button radius'])}}
@endif
@endif
Now, if I am watching other users' profiles everything is fine(if statement just works and I can see Follow link), However if I try to watch my own profile, laravel throws an exception : Trying to get property of non-object. The thing here is that $profile->user()->find(1)->id throws that exception, because when I hardcoded that to an integer everything worked properly.
Here is the line which causes the exception:
<?php if($authedUser->id !== $profile->user()->find(1)->id): ?>
PS1: The problem is not nested ifs.
PS2: In this situation my code never touch else part.
EDIT: Here is ProfileController@show :
public function show($userId)
{
try{
$profile = $this->profileRepo->byForeignKey('user_id',$userId)->firstOrFail();
}catch(ModelNotFoundException $e){
throw new ProfileNotFoundException('profile not found');
}
return View::make('profiles.show')->with('profile', $profile);
}
From comments: "because I need user's id. $profile->user() returns BelongsTo object so I used find(1) to get User object."
In this case you can use the dynamic property user instead of calling user().
@if($authedUser->id !== $profile->user->id)
Which is the same as
@if($authedUser->id !== $profile->user()->get()->id)
But you can't use find(1) here. It will try to search for an object with id = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With