2014-11-20 53 views
0

我的路線代碼調用未定義的方法照亮數據庫雄辯收藏::有()

return View::make('test')->with('foo', foo::all()->with('foos', 'bars')); 

這是爲什麼拋出此異常?

+1

類集合沒有辦法'with' http://laravel.com/據我所知,api/4.2/Illuminate/Database/Eloquent/Collection.html'foo :: all()'返回Collection(所有實體,就像一個數組),而不是查詢Builder實例http://laravel.com/api /4.2/Illuminate/Database/Eloquent/Builder.html – Cheery 2014-11-20 19:20:33

+0

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_with – 2014-11-20 19:24:58

+1

是的,有一個'with'方法,它是一個Builder類的方法,而不是Collection類的方法。集合(由'all()'獲得)是執行查詢的結果,包含來自數據庫的所有數據,而不是應該請求它的查詢。 – Cheery 2014-11-20 19:26:34

回答

2

函數all()執行查詢,以便獲取集合。您必須在查詢生成器實例上調用with。這意味着你應該這樣做:

foo::with('foos', 'bars')->get(); 

而且,上課的時間與約定一個大寫字母,所以應該是Foo;)

相關問題