在laravel中加入find_in_set [英] find_in_set with join in laravel

查看:1934
本文介绍了在laravel中加入find_in_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用laravel查询获取期望输出.尝试过这种方式无法获得成功,请提前多谢指导

How to get the desire output using laravel query. Tried that way does not get success please guide thanks a lot in advance

如果可以的话,有什么方法可以在模型中进行设置

Is there any way we can set it in model if possible please guide

User

id  name    b_id
1   Alax    1,3
2   Rex     2,4
3   Lex     2,3

Books

id  book_name   book_author
1   Javascript  jim
2   PHP         json
3   LARAVEL     rax
4   SYMPHONY    Alax

Output

id  name    b_id
1   Alax    Javascript, LARAVEL
2   Rex PHP, SYMPHONY
3   Lex PHP, LARAVEL

并查询:

$res = DB::table('user')->leftJoin('book', function($join){
   $join->on(DB::raw("find_in_set(book.id, user.b_id)",DB::raw(''),DB::raw('')));
});

推荐答案

尝试一下.

$data = \DB::table("user")
        ->select("user.*",\DB::raw("GROUP_CONCAT(book.book_name) as book_name"))
        ->leftjoin("book",\DB::raw("FIND_IN_SET(book.id,user.b_id)"),">",\DB::raw("'0'"))
        ->get();


\DB::raw("GROUP_CONCAT(CONCAT(book.book_name, ' - ', book.book_author) SEPARATOR ', ') AS book_name_author")

您的输出看起来像

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [0] => stdClass Object
                (
                    [id] => 1
                    [name] => Alax
                    [b_id] => 1,3
                    [book_name] => Javascript,LARAVEL
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [name] => Rex
                    [b_id] => 2,4
                    [book_name] => PHP,SYMPHONY
                )
            [2] => stdClass Object
                (
                    [id] => 3
                    [name] => Lex
                    [b_id] => 2,3
                    [book_name] => PHP,LARAVEL
                )

        )

)

这篇关于在laravel中加入find_in_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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