How can I convert the following SQL query into Laravel query builder? Can anyone help?
SELECT email,user from (select email,user_id as user from businesses ) as ui WHERE user > 1
Use this:
DB::query()->select('email', 'user')
->fromSub(function($query) {
$query->select('email', 'user_id as user')
->from('businesses');
}, 'ui')
->where('user', '>', 1)
->get();
Related
I'm trying to convert my sql query to Laravel Query
From this
SELECT products.name,
(SELECT SUM(orders.date BETWEEN from_date AND to_date)
FROM orders WHERE orders.product_id = products.id ) as order_counts
FROM products;
Then I tried converting to laravel query builder but it results to an error:
Product::select('name',
DB::raw("SUM(orders.date
BETWEEN $request->from_date AND $request->to_date) AS total_orders
FROM orders WHERE orders.product_id = products.id"))
->get();
The error is syntax error on the $request->from_date inside db::raw.
You can use the withCount method: Documentation
Product::select('name')->withCount([
'orders' => function (Builder $query) using ($from_date, $to_date) {
$query->whereBetween('date', [$from_date, $to_date]);
}
]);
I saw you have edited your issue, and describe your error. When using variables in string you should put them in braces.
Product::select('name',
DB::raw(
"SUM(orders.date BETWEEN {$request->from_date} AND {$request->to_date}) AS total_orders FROM orders WHERE orders.product_id = products.id"
)
)->get();
How do I convert this MySQL query to Laravel Eloquent Query:
SELECT * FROM service_package WHERE price IN ('110010', 'Test 02', '11009')
You can use the whereIn() like:
Using DB::table()
$data = \DB::table("service_package")
->whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Using Eloquent
$data = App\ServicePackage::whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Here, make sure you have created model ServicePackage for the table service_package.
SELECT * FROM posts WHERE user_id IN (SELECT following FROM follows WHERE user_id='1'
This is the query i want to wirte in laravel to fetch the data how can i write this in laravel
This should work:
$result = DB::table('posts')
->whereIn('user_id', function ($query) {
$query->select('following')
->from('follows')
->where('user_id', 1);
})->get();
I have a problem when my SQL on postgresSQL will be implement to Laravel
The query is
SELECT
ID AS id_bpr,
0 AS bpr_cabang,
bpr
FROM
m_bpr
WHERE
ID != 0
The query has been succesfull proced on Navicat, but when i try to implement in Laravel that query not working. This is code in laravel.
$allBpr = DB::table('m_bpr')
->select('id as id_bpr',
'0 as bpr_cabang',
'bpr')
->where('id', '!=', 0)
->get();
Why i implement that query to laravel?
Laravel will try to parse all fields in the SELECT part of your query. Because 0 is not a valid column, this will fail.
Wrap the option in \DB::raw() to tell Laravel to not parse the string.
$allBpr = DB::table('m_bpr')
->select(
'id as id_bpr',
\DB::raw('0 as bpr_cabang'),
'bpr'
)
->where('id', '!=', 0)
->get();
you are passing wrong parameter. pass ID like in above query instead of id
->where('ID', '!=', 0)
SELECT itemid,COUNT(itemid),itemname FROM `items`
WHERE DATE(`save_at`) ='2015-12-16'
GROUP BY itemid
Anyone know how to write this sql in laravel 5 using query builder ???
this is the code i used but it didn't work for me
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DATE(`save_at`),'=', 2015-12-16)
->group_by('itemid')
->get();
Check the below code.
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DB::raw('DATE(`save_at`)'),'=', '2015-12-16')
->groupBy('itemid')
->get();