Trying to get property of non-object (View: C:\laragon\www\blog\resources\views\admin\users\index.blade.php)

8.2k Views Asked by At

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.

5

There are 5 best solutions below

1
Max Almonte On

The store method seems to be good, the problem could be with the index method that returns your view with a list of users but not with the profile relation, you could try calling $user->profile()->avatar in your index view instead $user->profile->avatar. That way you're querying the relationship at the time of accessing it.

0
Ahmad ul hoq Nadim On

Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

0
Bonish Koirala On

You can try this instead

$user->profile($id)['avatar'];

considering that the input for your profile function is $id and the function returns the user profile information that you are fetching

1
Wajeeha Ehsan On

This sounds like your object does not exist OR that it is an array... Just add @ before your $user

 {{asset(@$user->profile->avatar)}}
0
Chetam Okafor On

from your post, I assume that you have a separate table for the profile, which is ideal. what you need to do is open your Profile model and create a relationship like this;

public function profile(){
    return $this->belongsTo('App\User', 'user_id', 'id');
}