Illuminate\Database\Eloquent\Collection::with() 方法未定义。

3
我的路由代码
 return View::make('test')->with('foo', foo::all()->with('foos', 'bars'));

为什么会抛出这个异常?

1
类 Collection 没有 with 方法 http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html foo::all() 返回 Collection(所有实体,类似于数组),据我所知,不是查询构建器实例 http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html - Cheery
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_with - S. M. Shahinul Islam
1
是的,有一个with方法,它是Builder类的方法,而不是Collection类的方法。 Collection(通过all()获得)是执行查询的结果,包含来自数据库的所有数据,而不是应该请求它的查询。 - Cheery
1个回答

11

all()函数执行查询以获取集合。您必须在查询构建器实例上调用with。这意味着您应该这样做:

all()函数执行查询以获取集合。您必须在查询构建器实例上调用with。这意味着您应该这样做:

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

另外,按照惯例,类名应以大写字母开头,所以应该是 Foo ;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接