dimanche 4 octobre 2015

Laravel - Eager Loading - Trying to get property of non-object

Sorry for the noob question, but I am not able to make eager loading work, and instead getting: Trying to get property of non-object in my view (index.blade.php below).

Basically I have the table credits with columns author and recipient both containing id from a users table.

I am trying to list credits, showing names of the authors and recipients (users).

Credit.php

class Credit extends Model {
    public function author() {
        return $this->belongsTo('App\User', 'author');
    }

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

User.php

class User extends Model {
    public function credits() {
        return $this->hasMany('App\Credit', 'recipient');
    }
}

CreditController.php

class CreditController extends Controller {
    public function index() {
        $credits = Credit::with('author','recipient')->get();
        return view('pages.credits.index')->withCredits($credits);
    }
}

index.blade.php

@foreach($credits as $credit)
    Author: {{ $credit->author->first_name }}
    Recipient: {{ $credit->recipient->first_name }}
@endforeach

What am I missing?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire