
基本上是伪代码:
All posts by user ::with('comments'). 要么
posts by Auth::user()->ID ::with('comments'). 我根据用户的表,注释表和帖子表,通常使用我的数据库设置.注释表具有一个post_ID,并且posts表具有一个user_ID.
没有Laravel的漫长的做法就是这样:
SELECT * FROM posts WHERE user_ID = 'user_ID'foreach($result as $post) { SELECT * FROM comments WHERE posts_ID = $post->ID foreach($query as $comment) { $result[$i]->comments[$n] = $comment }} 但是我想用Laravel的雄辩的ORM来完成它.
看起来你甚至不需要嵌套的加载,你只需要修改与返回的查询,所以:$posts = Post::with('comments')->where('user_ID','=',1)->get(); 您可以在Eloquent系统中菊花链链接大多数方法,通常它们只是返回一个Fluent查询对象.
(我没有测试过,但我相当肯定会工作,而且,你不能在:: all()上执行它,因为调用 – > get(),你必须挖掘源代码找到这个,我不认为这个雄辩的文档提到这是正在做的.)
另外Eager Loading Documentation还包含了嵌套的加载,所以你可以加载所有的用户,他们的帖子和评论:
You may even eager load nested relationships. For example,let’s
assume our Author model has a “contacts” relationship. We can eager
load both of the relationships from our Book model like so:
$books = Book::with(array('author','author.contacts'))->get(); 总结 以上是内存溢出为你收集整理的php – 一对多,然后渴望加载一个数组与Laravel雄辩ORM全部内容,希望文章能够帮你解决php – 一对多,然后渴望加载一个数组与Laravel雄辩ORM所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)