Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute MYSQL query in laravel?

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');
like image 686
Makerz Avatar asked Mar 31 '16 10:03

Makerz


2 Answers

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

like image 100
Tomer Ofer Avatar answered Nov 11 '22 06:11

Tomer Ofer


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');
like image 2
oseintow Avatar answered Nov 11 '22 08:11

oseintow