查询回调中的返回关键字 [英] Return keyword in query callback

查看:73
本文介绍了查询回调中的返回关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel中使用whereHas时是否有必要编写return关键字.

Is it necessary to write return keyword while using whereHas in laravel.

例如返回$ query-> where('status',1);

For eg. return $query->where('status', 1);

$posts = App\Post::whereHas('comments', function ($query) {
    $query->whereHas('content', function ($query){
       $query->where('status', 1);
    });
})->get();

我们是否需要在每个查询回调中编写return?

Do we need to write return in every query callback?

推荐答案

否,您正在通过调用$query->where('status', 1);修改传递给闭包的查询生成器实例.由于对象是通过引用传递的,并且where()会使该实例发生突变,因此无需返回任何内容.

No, you're modifying the query builder instance passed to your closure by calling $query->where('status', 1);. Since objects are passed by reference and where() mutates this instance, there's no need to return anything.

这篇关于查询回调中的返回关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆