mercredi 22 juin 2016

laravel assocation issue with user model

i have the association ads table with users how i can show the users data it shows me error trying to get property of non object association. tabels

    1)ads
    2)users(deafult Laravel)



    In User Model

    public function ads()
    {
        return $this->hasMany('App\Ad','user_id');
    }
    In Ad Model
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    in Routes
Route::get('/ads',function(){
      $ads = Ad::get();
      //print_r($ads);
   foreach ($ads as $ad) {
          echo $ad->user->name;// issue here

   }
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire