when I'm going to create a new user I get this type of error. And error mark over the image link. If I change image route like $user->profile['avatar']) it works, but in view page, it doesn't display the image.
view page:
<tbody>
@if($users->count() > 0)
@foreach($users as $user)
<tr>
<td>
<img src="{{asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%;">
</td>
<td>
{{ $user->name }}
</td>
<td>
@if($user->admin)
<a href="{{route('user.not.admin',['id' => $user->id])}}" class="btn btn-sm btn-outline-danger">Remove permission</a>
@else
<a href="{{route('user.admin',['id' => $user->id])}}" class="btn btn-sm btn-outline-primary">Make admin</a>
@endif
</td>
<td>
delete
</td>
</tr>
@endforeach
@else
<tr>
<th colspan="5" class="text-center">No users available!</th>
</tr>
@endif
</tbody>
</table>
Controller function:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email'
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt('password')
]);
$profile = Profile::create([
'user_id' => $user->id,
'avatar' => 'uploads/avatars/1.png'
]);
Session::flash('success', 'User created successfully!');
return redirect()->route('users');
}
Please help me to fix this problem. If you need more information about the code please let me know.
The
storemethod seems to be good, the problem could be with theindexmethod that returns your view with a list of users but not with theprofilerelation, you could try calling$user->profile()->avatarin your index view instead$user->profile->avatar. That way you're querying the relationship at the time of accessing it.