php - name - trying to get property of non-object laravel passport




Trying to get property of non-object-Laravel 5 (5)

I'm trying to echo out the name of the user in my article and I'm getting the ErrorException: Trying to get property of non-object . My codes:

Models

1. News

    class News extends Model
    {
      public function postedBy()
      {
         return $this->belongsTo('App\User');
      }
      protected $table = 'news';
      protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
    }

2. User

    class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;

        protected $table = 'users';

        protected $fillable = ['name', 'email', 'password'];

        protected $hidden = ['password', 'remember_token'];

    }

Schema

table users

table news

Controller

public function showArticle($slug)
    {
        $article = News::where('slug', $slug)->firstOrFail();
        return view('article', compact('article'));
    }

Blade

{{ $article->postedBy->name }}

When I try to remove name in the blade {{ $article->postedBy }} it outputs the id , but when I try to add the ->name there it says Trying to get property of non-object but I have a field name in my table and a User model. Am I missing something?


I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo . Here it is:

First, as suggested by Jimmy Zoto, my code in blade from $article->poster->name to $article->poster['name'] . Next is to add a second parameter in my belongsTo , from return $this->belongsTo('App\User'); to return $this->belongsTo('App\User', 'user_id'); in which user_id is my foreign key in the news table.

Thanks for all your help!


I had also this problem. Add code like below in the related controller (e.g. UserController)

$users = User::all();
return view('mytemplate.home.homeContent')->with('users',$users);

If you working with or loops ( for , foreach , etc.) or relationships ( one to many , many to many , etc.), this may mean that one of the queries is returning a null variable or a null relationship member.

For example : In a table, you may want to list users with their roles .

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->role->name }}</td>
    </tr>
    @endforeach
</table>

In the above case , you may receive this error if there is even one User who does not have a Role. You should replace {{ $user->role->name }} with {{ !empty($user->role) ? $user->role->name:'' }} , like this:

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
    </tr>
    @endforeach
</table>

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 (->).


Laravel optional() Helper is comes to solve this problem. Try this helper so that if any key have not value then it not return error

foreach ($sample_arr as $key => $value) {        
      $sample_data[] = array(       
        'client_phone' =>optional($users)->phone        
      );
    }
 print_r($sample_data);




laravel