Latheesan
Latheesan

Reputation: 24116

Eager loading not working in L4

I have the following tables:

users

    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username', 30);
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('remember_token')->nullable();
        $table->timestamps();
    });

organisations

    Schema::create('organisations', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name')->unique('name');
        $table->integer('owner_id')->unsigned()->index()->nullable();
        $table->foreign('owner_id')->references('id')->on('users');
        $table->timestamps();
    });

and I have the following Organisation Eloquent model:

class Organisation extends Eloquent {

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function owner()
    {
        return $this->belongsTo('User', 'owner_id', 'id');
    }

}

I am trying to use Eager Loading functionality of Eloquent ORM like this in my controller:

public function index()
{
    return View::make('organisations.index')
        ->with('organisations', Organisation::with('user')->all());
}

When I do this, I get the following exception error:

Call to undefined method Illuminate\Database\Query\Builder::all()

Any idea why this isn't working? Am I using the eager loading incorrectly?

Upvotes: 0

Views: 42

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152900

all() is a static method in the Model class, you can only use it like this: Model::all().
You need to use get() to execute your query.

Organisation::with('owner')->get();

Upvotes: 2

Related Questions