mercredi 6 avril 2016

Fetching records from second table in one to many relationship produce error in laravel 5.2

I have two tables, one is for approved projects and the other one is for artists details. i want to fetch data of "project name" data which is in the "project table" and "artists name" which is in the "artists table".

i have created the two relationships which are

ARTIST MODEL RELATIONSHIP

public function approved_relation()
{
    return $this->hasMany('App\approved_projects_model', 'artist_id');
}

APPROVED MODEL RELATIONSHIP

 public function artists_relation()
{
    return $this->belongsTo('App\artists_details_model', 'id');
}

In my controller, i have written the following code

$art_artist_details = approved_projects_model::with('artists_relation')->get();


   foreach ($art_artist_details as $artists_relationn)
     {
        echo $artists_relationn->category;

        foreach ($art_artist_details as $artists_relationn){

              echo $artists_relationn->artists_relation->phone_no;
          }             
 }

The problem is that the Foreach loops only once but it gives an error when the second loop reaches the echo $artists_relationn->artists_relation->phone_no;

after that it gives the following error "Trying to get property of non-object" I have done my research but its like i have reached a dead end. please assist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire