How to execute raw queries with Laravel 5.1?

PhpMysqlSqlDatabaseLaravel 5.1

Php Problem Overview


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

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();

I'm getting the error

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

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

Php Solutions


Solution 1 - Php

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");

Solution 2 - Php

    DB::statement("your query")

I used it for add index to column in migration

Solution 3 - Php

Example from Laravel documentation:

$users = DB::table('users')
    ->selectRaw('count(*) as user_count, status')
    ->where('status', '<>', 1)
    ->groupBy('status')
    ->get();

Another example:

$products = DB::table('products')
    ->leftjoin('category','category.product_id','=','products.id')
    ->selectRaw('COUNT(*) as nbr', 'products.*')
    ->groupBy('products.id')
    ->get();

Another example – we can even perform avg() and count() in the same statement.

$salaries = DB::table('salaries')
    ->selectRaw('companies.name as company_name, avg(salary) as avg_salary, count(*) as people_count')
    ->join('companies', 'salaries.company_id', '=', 'companies.id')
    ->groupBy('companies.id')
    ->orderByDesc('avg_salary')
    ->get();

Credits: https://blog.quickadminpanel.com/5-ways-to-use-raw-database-queries-in-laravel/

Solution 4 - Php

you can run raw query like this way too.

DB::table('setting_colleges')->first();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSandro WiggersView Question on Stackoverflow
Solution 1 - PhpSandro WiggersView Answer on Stackoverflow
Solution 2 - PhpДароваДароваView Answer on Stackoverflow
Solution 3 - PhpGediminasView Answer on Stackoverflow
Solution 4 - Phppankaj kumarView Answer on Stackoverflow