I have one MYSQL query with me I want to execute this query in laravel.
select d1.update_id from ( select update_id, count(update_id)
as ct from updates_tags where tag_id in
(67,33,86,55) group by update_id) as d1 where d1.ct=4
Please guide me how do i Do it easily.
I have one reference with me.... It is not working
$updateArray = DB::table('updates_tags')
->select('update_id', DB::raw('count(*) as update_id'))
->whereIn('tag_id',$jsontags)
->groupBy('update_id')
->having('update_id','=',count($jsontags))
->lists('update_id');
In classic PHP, we used to use mysqli
connector. In CodeIgniter, we use ActiveRecords (fully lovely). In Laravel, there is also ActiveRecords for SQL queries.
Checkout the official Laravel docs, query builder is your friend. https://laravel.com/docs/4.2/queries
change the where('total','=',count($tags)) to having('total','=',count($tags))
$update = DB::table('tags')
->select('t_id', 'u_id', DB::raw('count(*) as total'))
->whereIn('t_id',$tags)
->groupBy('u_id')
->having('total','=',count($tags))
->lists('u_id');
You can just do a RAW query. like this:
$sqlQuery = "SELECT d1.update_id FROM...";
$result = DB::select(DB::raw($sqlQuery));
the $result will be an array