By Yusuf Borucu


2018-08-10 11:48:06 8 Comments

I would to convert mysql query to laravel query.

MySQL:

select product_id as urun from product_features 
   where feature_id = 58 
      or feature_id = 100 
      or (feature_id = 52 or feature_id = 48 or feature_id = 53)      
   group by product_id having(count(product_id)>2)

Edit: I can not enclose 'or' expressions in parentheses.

2 comments

@Yusuf Borucu 2018-08-10 12:10:40

I solved the problem.

$query = DB::table('product_features')->where('feature_id', 52)->orWhere('feature_id', 48)->orWhere('feature_id', 53)->
    orWhere('feature_id', 100)->orWhere('feature_id', 58)->
    select('product_id')->groupBy('product_id')->havingRaw("count(product_id) > 2")->get();

@ceejayoz 2018-08-10 12:12:13

While this will work in your particular case, this won't produce the parenthesis, which will cause issues in more complicated queries. See my answer for how you'd do parenthetical sections of a query.

@Yusuf Borucu 2018-08-10 12:20:18

I will do as you say. Thank you.

@ceejayoz 2018-08-10 12:10:38

Edit: I can not enclose 'or' expressions in parentheses.

Sure you can. :-)

What you'll want to look at is Laravel's parameter grouping functionality.

$query->orWhere(function($query) {
    $query->where('foo', 'bar')
          ->orWhere('foo', 'not-bar');
});

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] How do I add indices to MySQL tables?

35 Answered Questions

[SOLVED] Should I use the datetime or timestamp data type in MySQL?

37 Answered Questions

[SOLVED] How to import an SQL file using the command line in MySQL?

11 Answered Questions

[SOLVED] Laravel - Eloquent or Fluent random row

11 Answered Questions

[SOLVED] MySQL - UPDATE query based on SELECT Query

1 Answered Questions

[SOLVED] sql isn't in GROUP BY using query builder laravel

  • 2018-07-16 16:42:49
  • Faris Dewantoro
  • 56 View
  • 1 Score
  • 1 Answer
  • Tags:   mysql laravel

27 Answered Questions

[SOLVED] How to output MySQL query results in CSV format?

  • 2008-12-10 15:59:51
  • MCS
  • 920444 View
  • 939 Score
  • 27 Answer
  • Tags:   mysql csv quotes

1 Answered Questions

[SOLVED] convert mysql to query to laravel query

14 Answered Questions

[SOLVED] How to get a list of MySQL user accounts

  • 2009-07-16 03:23:53
  • burntsugar
  • 1350058 View
  • 1216 Score
  • 14 Answer
  • Tags:   mysql mysql5

15 Answered Questions

[SOLVED] Error related to only_full_group_by when executing a query in MySql

Sponsored Content