Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 雄辩的芬多尔谈恋爱_Laravel_Eloquent - Fatal编程技术网

Laravel 雄辩的芬多尔谈恋爱

Laravel 雄辩的芬多尔谈恋爱,laravel,eloquent,Laravel,Eloquent,假设$post是一个雄辩的post模型,它有许多与之相关的Comment模型 以下是post模型: /** *获取评论。 */ 公共职能评论() { 返回$this->hasMany('App\Models\Comment'); } 我试图通过帖子的id检索属于帖子的评论。如果注释存在,运行以下代码将返回注释,但不考虑它是否属于该帖子(关系)。这允许用户检索评论,即使他们不在当前帖子上 $comment=$post->comments()->findOrFail($commentId); 以

假设
$post
是一个雄辩的
post
模型,它有许多与之相关的
Comment
模型

以下是post模型:

/**
*获取评论。
*/
公共职能评论()
{
返回$this->hasMany('App\Models\Comment');
}
我试图通过帖子的
id
检索属于帖子的评论。如果注释存在,运行以下代码将返回注释,但不考虑它是否属于该帖子(关系)。这允许用户检索评论,即使他们不在当前帖子上

$comment=$post->comments()->findOrFail($commentId);
以下内容首先返回一个集合,然后通过
id
找到正确的注释

$comment=$post->comments->find($commentId);
但是,在尝试访问注释属性时,我需要通过查询生成器使用
findOrFail()
方法而不是集合上的
find()
方法来返回异常,而不是获取
未定义错误。

您可以执行以下操作:

//获取帖子:
//$post=post::find($posted);
//获取评论。
$comments=$post->comments()->where('id',$commentId)->get();//如果他们很多的话。
//$comment=$post->comments()->where('id',$commentId)->first();//如果只有一个。
if(空($comments))/“if($comment)”,如果它只是一个。
{
中止(404,‘注释不存在’);
}
返回视图('some.view')->带有注释($comments);
然后在您的视图中使用
$comments


更新: 试试这个:

$post->load('comments',函数($query)use($commentId){
$query->findOrFail($commentId);
});
然后:

returnview('some.view')->withComments($post->comments);
尝试更换

$comment = $post->comments()->findOrFail($commentId);


但是,我假设您的注释id是唯一的,无论您是通过post表进行透视还是直接访问它……

如果不查看您的关系模型,就很难准确地说出您的注释id。但是,用
findOrFail
替换该查找怎么样。这不管用吗?如果没有,请说明您是如何定义关系的。@SarojShrestha在最后一行中用findOrFail替换find返回:Method Illumb\Database\Eloquent\Collection::findOrFail不存在。我已经用模型定义更新了我的原始帖子。
findOrFail()
适合我。是否可以使用
\DB::enableQueryLog()记录执行的查询;[…]dd(\DB::getQueryLog())?我想如果您只想给出一条注释,那么
first()
方法应该更合适。您可以使用
->comments()->where('comments.id',$commentId)->first()
这样做怎么样?这是可行的,但我更愿意使用findOrFail方法在一行中完成所有操作。@Dynamic我已经更新了答案。不知道这是否有效,但还没有测试。让我知道这是否有效。这将允许我访问不属于特定帖子模型的评论。应该没有问题,因为您已经以某种方式计算了$commentId。。。如果您仍然认为这会让您访问错误的注释,您可以执行类似Comment::where('post_id',$post->id)->findOrFail($commentId)的操作
$comment = \App\Models\Comment::findOrFail($commentId);