mardi 18 juillet 2017

Laravel Eloquent relationship not working

I am using laravel 5.4

I have two models User Model and Experience Model. I have created Experience method inside the User model like below.

public function Experience() {
    return $this->hasMany('App\Experience', 'updatedbyuser_id');
}

Here updatedbyuser_id is the foreign key and is a column of the experiences table in the database.

I have created another method called User within the Experience model like below.

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

I have created a controller 'PagesController' and placed the below method in it

public function returnExperience() {
    $lists = Experience::where(['deleted' => '0'])->get();
    return view('pages.experience', compact('lists'));
}

To view the data returned by the returnExperience() method my view page has the below code

              @foreach($lists as $list)
                <tr>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td>Edit</td>
                </tr>
              @endforeach

but it returns the below error message on the screen

Object of class Illuminate\Database\Eloquent\Relations\BelongsTo could not be converted to string



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2uuKNOH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire