0

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?

Community
  • 1
  • 1
Latheesan
  • 23,247
  • 32
  • 107
  • 201

1 Answers1

2

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();
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • Thanks. This almost worked. What I needed was this: `Organisation::with('owner')->get();` as my `belongsTo` relationship was defined in the `owner()` method on the `Organisation` model. – Latheesan Jan 20 '15 at 12:20
  • 1
    A yes. I honestly wasn't even looking at your relations. I only saw the error and your call and knew what the problem was ;) – lukasgeiter Jan 20 '15 at 12:23