社区评论 Part 1
打赏作者

天山网艾克

这里的评论如何分页呢?

JellyBool 回复 天山网艾克

你直接就

$discussion->comments()->paginate(16);
// $discussion->comments->paginate(16);

试试

天山网艾克 回复 JellyBool

3Q, 我试一试

lzy13121013 回复 JellyBool

站长你好,用discussion->comments()->paginate(16)这种方法确实可以,但是在视图层显示页码的时候。怎么表示?discussion->comments()->links()这种写法不行呀。

JellyBool 回复 lzy13121013

可能是 laravel 版本不一样吧。后面就变成 render 了:

$discussion->comments()->render()

注意一下版本就行

lzy13121013 回复 JellyBool

{$discussion->comments()->render()}这样写不行。call to undefined method.咋回事呀站长。这个问题弄了好久了。。。

JellyBool 回复 lzy13121013

你的 laravel 版本是多少 啊

lzy13121013 回复 JellyBool

5.4的版本啊

JellyBool 回复 lzy13121013

这样的吧:

$comments = $discussion->comments()->paginate(16)

视图输出:

{ $comments->links() }

注意传递 $comments

lzy13121013 回复 JellyBool

谢谢站长你真棒

JellyBool 回复 lzy13121013

没得事,问题解决了就好

天山网艾克

我的业务逻辑是评论是审核后显示,所以我增加了status字段,如果status=1 才可以正常显示。如何跟belongsto 联合使用?

天山网艾克

hasMany(comments:class)->where(‘status’,1); 这样写可以吗?

JellyBool 回复 天山网艾克

可以的,直接这样应该没有问题

天山网艾克

我看了查询语句,这样使用belongsto等语句后,查询量增加好多呀。读取评论表,如果有10个评论,再10次去读取用户表。。。

JellyBool 回复 天山网艾克

使用 with 就可以避免这种问题了。

天山网艾克 回复 JellyBool

有关于with的视频教程吗

MarksGui888 回复 天山网艾克
在PostController@show方法中添加代码并传值到模板页面:
$comments = Comment::where('discussion_id',$id)->with('user')->get();
再把foreach里面循环的值改为 $comments即可

MarksGui888 回复 天山网艾克

记得多去看看laravel手册,关联关系预加载有提到这个点

sbtaxj 回复 MarksGui888

回复得很好,棒棒的

yuefei12

数据库里的快件已经存在了,但是调用方法的时候没有数据为什么啊?我用的是5.1版本的

JellyBool 回复 yuefei12

具体的代码是什么样的

yuefei12 回复 JellyBool

谢谢啊,已经解决了,版本问题

hate

Illuminate\Database\QueryException with message ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.comments, CONSTRAINT comments_discussion_id_foreign FOREIGN KEY (discussion_id) REFERENCES discussions (id) ON DELETE CASCADE) (SQL: insert into comments (body, user_id, discussion_id, updated_at, created_at) values (Ratione adipisci numquam eos accusamus velit. Quod quod nulla velit voluptatem fuga. Ut sit veritatis aliquam quod rem autem occaecati., 138, 150, 2018-05-14 04:03:21, 2018-05-14 04:03:21))’

hate

这什么问题呢,找不到

JellyBool 回复 hate

discussion_id 是不是没有?或者这个 discussion_id 在 discussions 这个表不存在?

hate 回复 JellyBool

discussion_id,不是discussions的主键吗。我是在填充100条数据的时候报错的

hanweizhe 回复 hate

emmmmmmmmmm