找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

介绍Laravel子查询语句用法

作者:模板之家 2021-1-28 18:44 79人关注

下面由Laravel教程栏目给大家介绍Laravel 子查询语句用法,希望对需要的朋友有所帮助!

下面由Laravel教程栏目给大家介绍Laravel 子查询语句用法,希望对需要的朋友有所帮助!

class UserController extends Controller{
    public function index()
    {
        $columns = ['id', 'name', 'email', 'created_at'];
        $users = User::addSelect([
            'last_post_title' => Post::select(['title'])
                ->whereColumn('user_id', 'users.id')
                ->where('status', Post::STATUS_NORMAL)
                ->orderByDesc('created_at')
                ->limit(1)
        ])->orderByDesc('id')->paginate(20, $columns);
        return view('user.index', ['users' => $users]);
    }}

addSelect 方法可用于添加一个查询字段到已存在的查询实例,我们通过传递一个数组 —— 数组键是返回的查询字段名,即 SQL 语句中的 last_post_title,数组值是对应的子查询逻辑,注意外键关联需要通过 whereColumn 方法设置,其他和正常 Eloquent 查询一样。

以上就是介绍Laravel子查询语句用法的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)