5

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');
5
  • please, precise what is not working, is there an error ? if yes what error, if no, what do you get as result ? Commented Mar 31, 2016 at 10:56
  • @n00dl3 : I am not able to arrange the query in the laravel format and it gives me the SQL query violation error While I am executing. Commented Mar 31, 2016 at 11:00
  • Did you tried to execute the single query in MySQL, without PHP/Laravel? I'm not sure, but... what if there is an error in your query? Commented Mar 31, 2016 at 11:12
  • code.tutsplus.com/tutorials/… Commented Mar 31, 2016 at 11:14
  • JuanjoSalvador: it is working fine with MySQL and it gives me output what i want. but not able to convert it in laravel. Commented Mar 31, 2016 at 11:18

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

Object of class stdClass could not be converted to string Giving this error.
where are you doing it? in the controller? try to print it' see what you get. use var_dump($result) or print_r($result)
Array ( [0] => stdClass Object ( [update_id] => 2 ) ) {"code":"","message":"","results":[]}
Array ( [2] => 2 ) array(1) { [0]=> object(stdClass)#651 (1) { ["update_id"]=> int(2) } }
so your query is incorrect, there is nothing to do with Laravel itself. show us the structure of your data then we can help you.
2

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

6 Comments

I am beginner in the laravel. Is it working same as MYSQL query. ? 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
the update_id has been used already so you cannot use it as your alias. Thats what you are using as the first select. So give it another name
Yes I am getting the right value from above laravel query it is return the update_id. $updateArray = UpdatesTags::whereIn('tag_id',$jsontags)->lists('update_id'); This is returning array but our query is not.
if you want it to return array add ->all() to the query. like this ->lists('u_id')->all();
Call to a member function all() on a non-object
|
0

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

3 Comments

I tried with official Laravel docs .. Not succeed that's y i come here for help.
Check your query, it may be a SQL mistake.
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 This MYSQL query gives me output what i want.. Which is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.