How to convert sql to Laravel Eloquent - php

Have specific SQL query need to convert to Laravel eloquent, I already converted getting error, does everyone help me pls?
my sql
select
`d1`.*, `a1`.`user_uuid` as `activity_user`
from
`directors` as `d1`
left join
`activities` as `a1`
on `a1`.`entity_uuid` = `d1`.`uuid`
and `a1`.`created_at` = (
select
max(`created_at`)
from
`activities` as `a2`
where `a2`.`entity_uuid`=`d1`.`uuid`
)
where `a1`.`user_uuid`='4f858f46-2d7e-41c8-a185-c9c4a4b506d1'
group by `a1`.`entity_uuid`
order by `d1`.`updated_at`
desc limit 100;
and my laravel eloquent
DB::table('directors')
->select('directors.*')
->leftJoin('activities',function($join) {
$join->on('a1.entity_uuid','=','directors.uuid')
->on('a1.created_at','=',function($query) {
$query->from('activities')
->select(DB::raw('max'('activities.created_at'))
->where('activities.entity_uuid','=',DB::raw('d1.uuid'));
});
}
->where('activities.user_uuid','=','4f858f46-2d7e-41c8-a185-c9c4a4b506d1')
->groupBy('activities.entity_uuid')
->orderBy('directors.updated_at','desc')
->get();
the error what I get
TypeError: stripos(): Argument #1 ($haystack) must be of type string, Closure given in file C:\xampp\htdocs\crmMembers\backend\vendor\laravel\framework\src\Illuminate\Database\Grammar.php on line 62

thanks for everyone, it solved
just change double ->on to ->where and works perfect
->where('a1.created_at', '=', function($q){$q->from('activities as a2')
->select(DB::raw('max(`a2`.`created_at`)'))
->where('a2.entity_uuidm', '=', DB::raw('`d1`.`uuid`'));
});

Your Laravel DB query should be like this
DB::table('directors')->select('directors.*', 'activities.user_uuid as activity_user')
->leftJoin('activities',function($join) {
$join->on('a1.entity_uuid','=','directors.uuid')
->on('a1.created_at','=',function($query) {
$query->from('activities')
->select(DB::raw('max(created_at)')
->where('activities.entity_uuid','=',DB::raw('d1.uuid'));
});
})
->where('activities.user_uuid','=','4f858f46-2d7e-41c8-a185-c9c4a4b506d1')
->groupBy('activities.entity_uuid')
->orderBy('directors.updated_at','desc')
->get();

Related

How to convert raw SQL query to Laravel Query Builder

I need a following code to convert to Laravel query can any one help me with these.
SELECT id, `leave_name`, `total_leave_days`, leave_id, leave_taken_days FROM `leaves` AS t1 INNER JOIN ( SELECT leave_id, SUM(`leave_taken_days`) AS leave_taken_days FROM `leave_applications` WHERE user_id = 2 AND statuses_id = 2 GROUP BY leave_id ) AS t2 ON t1.id = t2.leave_id
I even tried but the output is not showing atall.
$user_leaves = DB::table('leaves')
->select('id', 'leave_name', 'total_leave_days', 'leave_id', 'leave_taken_days')
->join('leave_application', 'leave_application.leave_id', '=', 'leave.id')
->select('leave_application.leave_id', DB::raw("SUM(leave_taken_days) as leave_application.leave_taken_days"))
->where('user_id','=', 2)
->where('statuses_id','=', 2)
->get();
How can I solve this issue?
UPDATE
Relations between two models.
Leave Model
public function leave_application()
{
return $this->belongsTo(LeaveApplication::class, 'id' , 'leave_id');
}
Leave Application Model
public function leave()
{
return $this->belongsTo(Leave::class, 'leave_id', 'id');
}
Try this :
$user_leaves = Leave::select('leaves.id', 'leaves.leave_name', 'leaves.total_leave_days', 'leave_applications.leave_id', DB::raw('SUM(leave_applications.leave_taken_days) as leave_taken_days'))
->with('leave_application')
->whereHas('leave_application', function($q) {
$q->where('user_id', 2)
->where('statuses_id', 2);
})
->groupBy('leaves.id')
->get();
On this topic I would like to give my recommendations for some tools to help you out in the future.
SQL Statement to Laravel Eloquent to convert SQL to Laravel query builder. This does a decent job at low level queries. It also saves time when converting old code.
The other tool I use to view the query that is being run is Clock Work
I keep this open in a tab and monitor slow nasty queries or, also gives me perspective on how the query builder is writing SQL. If you have not use this extension I highly recommend getting and using it.
Actually I found my answer,
$user_leaves = DB::table('leaves as t1')
->select('t1.id', 't1.leave_name', 't1.total_leave_days', 't2.leave_id', 't2.leave_taken_days')
->join(DB::raw('(SELECT leave_id, SUM(leave_taken_days) AS leave_taken_days FROM leave_applications WHERE user_id = ' . $user_id . ' AND statuses_id = 2 GROUP BY leave_id) AS t2'), function ($join) {
$join->on('t1.id', '=', 't2.leave_id');
})
->get();
You can use DB:select("your query", params) and put your query and params (as an array (optional)
As below sample:
$result = DB:select("
SELECT id, `leave_name`, `total_leave_days`, leave_id, leave_taken_days
FROM `leaves` AS t1
INNER JOIN (
SELECT leave_id, SUM(`leave_taken_days`) AS leave_taken_days
FROM `leave_applications`
WHERE user_id = 2
AND statuses_id = 2
GROUP BY leave_id
) AS t2 ON t1.id = t2.leave_id" , $params
);
return response()->json($result);

laravel access outer query column inside subquery

I am trying to convert raw sql queries into laravel queries.
Here's the raw query:
select
tsk.id,
tsk.request_id,
tsk.sys_index,
tsk.category_group,
tsk.category,
tsk.is_assigned,
tsk.hash_id
from
user_tasks as usr
inner join
unassigned_tasks as tsk
on usr.task_id = tsk.id
where
usr.assigned_to = 12
AND
tsk.product_id NOT IN ( SELECT product_id FROM product_progresses WHERE request_id = tsk.request_id )
AND
BINARY hash_id NOT IN ( SELECT hash_id FROM product_match_unmatches WHERE request_id = tsk.request_id AND auto_unmatched_by IS NOT NULL )
The laravel query is:
public function getTasks($assigned_to) {
/** fetch products assigned to a specific user token,
* ignore already matched skus, and links that are auto-unmatched
**/
$tasks = DB::table('user_tasks as usr')
->join('unassigned_tasks as tsk', 'usr.task_id', '=', 'tsk.id')
->select('tsk.id', 'tsk.request_id', 'tsk.sys_index', 'tsk.category_group', 'tsk.category', 'tsk.is_assigned', 'tsk.hash_id')
->where('usr.assigned_to', '=', $assigned_to);
$tasks->whereNotIn('tsk.product_id', function($qs) {
$qs->from('product_progresses')
->select(['product_id'])
->where('request_id', '=', 'tsk.request_id')
->get();
});
$tasks->whereNotIn(DB::raw('BINARY `hash_id`'), function($qs) {
$qs->from('product_match_unmatches')
->select('hash_id')
->where('request_id', '=', 'tsk.request_id')
->whereNotNull('auto_unmatched_by')
->get();
});
return $tasks->toSql();
The below query should take tsk.request_id value from outer query, but I think the column value is not passed to it.
Here's the output of toSql():
SELECT `tsk`.`id`,
`tsk`.`request_id`,
`tsk`.`sys_index`,
`tsk`.`category_group`,
`tsk`.`category`,
`tsk`.`is_assigned`,
`tsk`.`hash_id`
FROM `user_tasks` AS `usr`
INNER JOIN `unassigned_tasks` AS `tsk`
ON `usr`.`task_id` = `tsk`.`id`
WHERE `usr`.`assigned_to` = ?
AND `tsk`.`product_id` NOT IN (SELECT `product_id`
FROM `product_progresses`
WHERE `request_id` = ?)
AND BINARY `hash_id` NOT IN (SELECT `hash_id`
FROM `product_match_unmatches`
WHERE `request_id` = ?
AND `auto_unmatched_by` IS NOT NULL)
Note the ? inside where clauses.
The resultset is different from the raw and laravel query.
I even tried see the bindings value:
//dd($tasks->getBindings());
$sql = str_replace_array('?', $tasks->getBindings(), $tasks->toSql());
dd($sql);
And on running this raw query, it is outputting the correct result-set.
UPDATE:
On checking the bindings, here's what I found:
array:3 [▼
0 => 12
1 => "tsk.request_id"
2 => "tsk.request_id"
]
Here outer query column is wrapped inside quotes and hence treated as a string.
So maybe where clause is trying to compare request_id with a string rather than the outer column.
If it is so, then how do I make them treat as columns rather than string?
use DB::raw() where you trying to add value of request_id
Example
AND `tsk`.`product_id` NOT IN (SELECT `product_id`
FROM `product_progresses`
WHERE `request_id` = DB::raw('tsk.request_id'))
whereRaw('pgr.request_id = tsk.request_id');
Solved the string issue.
You should try to remove select() method, in the subquery replace where() method with whereColumn() method and remove get() method:
$tasks = DB::table('user_tasks', 'urs')
->join('unassigned_tasks as tsk', 'usr.task_id', '=', 'tsk.id')
->where('usr.assigned_to', '=', $assigned_to);
Note: i put the alias 'urs' as second argument (view docs)
$tasks->whereNotIn('tsk.product_id', function($qs) {
$qs->from('product_progresses')
->select(['product_id'])
->whereColumn('request_id', 'tsk.request_id');
});
If you want get specific fields, you must specify the fields in get() method:
return $tasks->get(array('tsk.id', 'tsk.request_id', 'tsk.sys_index', 'tsk.category_group', 'tsk.category', 'tsk.is_assigned', 'tsk.hash_id'));

i want this query in laravel

I want to know how to write an exact query in laravel using joins, please help somebody to solve this issue
select tickets.* from `employees` inner join `tickets` on
`employees`.`user_id` = `tickets`.`emp_id` inner join `categories` on
`categories`.`acc_id` = `employees`.`acc_id` and `categories`.`id` =
(`tickets`.`cat_id` or `tickets`.`cat_id`=0) where (`employees`.`user_id` = 11)
If you are using plain query builder,
$result = DB::select(DB::raw("tickets.*"))
->join("tickets","employees.user_id","=","tickets.emp_id")
->join('categories', function ($join) {
$join->on("categories.acc_id","=","employees.acc_id");
$join->on("categories.id","=",DB::raw("(tickets.cat_id or tickets.cat_id=0)")
})->where("employees.user_id",11)->get();
Since the Answer by #Drakula Predator is good
But I avoided using the Raw queries
$laravelWay = DB::table('employees')
->select('tickets.*')
->join('tickets','employees.user_id','=','tickets.emp_id')
->join('categories', function ($join) {
$join->on('categories.acc_id','=','employees.acc_id')
->on('categories.id','=','tickets.cat_id')
->orOn('tickets.cat_id','=','0');
})
->where('employees.user_id','=',11);

Comparing timestamps from two different tables in Laravel 5

I am trying to get results out of union by showing only results that were updated after I last checked (defined by $last_check)
$last_check = CheckNotifications::where('notif_check_user_id', '=', $user_id)->pluck('updated_at');
$get_projects = DB::table('requests')
->join('notifications', 'notifications.notif_project_id', '=', 'requests.id')
->select(DB::raw('requests.updated_at, requests.request_name, "" as fullname, "P" as flag'))
->where('notif_user_id', '=', $user_id)
->whereRaw('requests.updated_at > requests.created_at');
$comments = DB::table('project_comments')
->leftJoin('users', 'comment_user_id', '=', 'users.id')
->leftJoin('requests', 'comment_project_id', '=', 'requests.id')
->select(DB::raw('project_comments.updated_at, requests.request_name, users.fullname, "C" as flag'));
$get_notifications = $get_projects->union($comments)
->whereRaw('updated_at > $last_check')
->orderBy('updated_at', 'desc')
->get();
The union works just fine until I add that last whereRaw statement. With the statement added, I get a SQL Syntax/Access error (#42000). I think it has something todo with the plucking of updated_at for the comparison. Any ideas? Thanks in advance!
Edit: Here's the error code
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:09:06) union (select project_comments.updated_at, requests.id as request_id, ' at line 1 (SQL: (select requests.updated_at, requests.id as request_id, requests.request_name, "" as fullname, "P" as flag from `requests` inner join `notifications` on `notifications`.`notif_project_id` = `requests`.`id` where `notif_user_id` = 1 and requests.updated_at > requests.created_at and requests.updated_at > 2015-07-29 15:09:06) union (select project_comments.updated_at, requests.id as request_id, requests.request_name, users.fullname, "C" as flag from `project_comments` left join `users` on `comment_user_id` = `users`.`id` left join `requests` on `comment_project_id` = `requests`.`id`) order by `updated_at` desc)
there whould be 2 updated_at column , Please attach your error while mentioning something like that again I can't be sure of the reason.
the solution is simply to write the table name before the column name
change
->whereRaw('updated_at > $last_check')
into
->whereRaw('requests.updated_at > $last_check')
and change orederby() to be after the whereRaw()
you had written the right code just few lines before :D
I hope this solve your problem
add that whereRaw clause for each of previous two queries
->where('requests.updated_at','>', $last_check)
or
->whereRaw('requests.updated_at > ?', [$last_check])
probably where is not available with union
just curious : if this is working?
$get_notifications = $get_projects->union($comments)
->where(function($query) use ($last_checked){
$query ->where('requests.updated_at','>', $last_check);
})
->orderBy('requests.updated_at', 'desc')
->get();

Laravel Eloquent using WHERE instead of AND in Joins

I have the following query:
select i.name,i.id itemId,its.item_id from items i
LEFT JOIN items_subscribers its
ON i.id = its.item_id
and its.user_id = 1
Which I transformed into Eloquent Way like this:
$records = Item::where('items_subscribers.user_id',$user_id)
->leftJoin('items_subscribers','items.id','=','items_subscribers.item_id')
->select(['items.name','items.id','items_subscribers.item_id as selected_item_id'])->get();
When I run print_r(DB::getQueryLog()); it prints following query:
select `items`.`name`, `items`.`id`, `items_subscribers`.`item_id` as `selected_item_id` from `items` left join `items_subscribers` on `items`.`id` = `items_subscribers`.`item_id` where `items_subscribers`.`user_id` = ?
The query yields nothing. I want to use AND instead of WHERE. How'd I do this?
I am using Laravel 4.2
Try using a closure and adding the condition in there:
$records = Item::leftJoin('item_subscribers', function($join) use ($user_id){
$join->on('items.id','=','items_subscribers.item_id');
$join->where('items_subscribers.user_id', '=', $user_id);
})
->select(['items.name','items.id','items_subscribers.item_id as selected_item_id'])
->get();
Just add another on method at the leftJoin part instead of using the where method. - https://stackoverflow.com/a/26714765/3208719

Categories