如何使用Laravel 5.1执行原始查询? [英] How to execute raw queries with Laravel 5.1?

查看:249
本文介绍了如何使用Laravel 5.1执行原始查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个小查询在我的数据库上运行,它在MySQL Workbench中正常工作。
基本上,LEFT JOIN的SELECT和LEFT JOIN的UNION。

So I have this tiny query to run on my DB and it works fine in MySQL Workbench. Basically, a SELECT with LEFT JOIN and UNION with LEFT JOIN again.

SELECT
    cards.id_card,
    cards.hash_card,
    cards.`table`,
    users.name,
    0 as total,
    cards.card_status,
    cards.created_at
FROM cards
LEFT JOIN users
ON users.id_user = cards.id_user
WHERE hash_card NOT IN ( SELECT orders.hash_card FROM orders )
UNION
SELECT
    cards.id_card,
    orders.hash_card,
    cards.`table`,
    users.name,
    sum(orders.quantity*orders.product_price) as total, 
    cards.card_status, 
    max(orders.created_at) 
FROM menu.orders
LEFT JOIN cards
ON cards.hash_card = orders.hash_card
LEFT JOIN users
ON users.id_user = cards.id_user
GROUP BY hash_card
ORDER BY id_card ASC

尝试将其翻译成Laravel,但没有成功。

In tried to translate it to Laravel, with no success.

$cards = Card::selectRaw('cards.id_card, cards.hash_card ,cards.table, users.name, 0 as total, cards.card_status, cards.created_at as last_update')
                ->leftJoin('users','users.id_user','=','cards.id_user')
                ->whereNotIn( 'hash_card', Order::select('orders.hash_card')->get() )
                ->union(
                        Order::selectRaw('cards.id_card, orders.hash_card, cards.table, users.name, sum(orders.quantity*orders.product_price) as total, cards.card_status, max(orders.created_at) as last_update')
                        ->leftJoin('cards','cards.hash_card','=','orders.hash_card')
                        ->leftJoin('users','users.id_user','=','cards.id_user')
                )
                ->groupBy('hash_card')
                ->orderBy('cards.id_card','asc')
                ->get();

我收到错误


Builder.php第1249行中的ErrorException:未定义的属性:
Illuminate \Database\Eloquent\Builder :: $ bindings

ErrorException in Builder.php line 1249: Undefined property: Illuminate\Database\Eloquent\Builder::$bindings

我如何在Laravel中执行完全原始的查询或以正确的方式在Laravel中写入查询?

How could I execute a completely raw query in Laravel or write the query in the right manner in Laravel?

推荐答案

我在此主题中找到了解决方案并且我编码:

I found the solution in this topic and I code this:

$cards = DB::select("SELECT
                                cards.id_card,
                                cards.hash_card,
                                cards.`table`,
                                users.name,
                                0 as total,
                                cards.card_status,
                                cards.created_at as last_update
                            FROM cards
                            LEFT JOIN users
                            ON users.id_user = cards.id_user
                            WHERE hash_card NOT IN ( SELECT orders.hash_card FROM orders )
                            UNION
                            SELECT
                                cards.id_card,
                                orders.hash_card,
                                cards.`table`,
                                users.name,
                                sum(orders.quantity*orders.product_price) as total, 
                                cards.card_status, 
                                max(orders.created_at) last_update 
                            FROM menu.orders
                            LEFT JOIN cards
                            ON cards.hash_card = orders.hash_card
                            LEFT JOIN users
                            ON users.id_user = cards.id_user
                            GROUP BY hash_card
                            ORDER BY id_card ASC");

这篇关于如何使用Laravel 5.1执行原始查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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