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

47 Answered Questions

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

14 Answered Questions

[SOLVED] Laravel - Eloquent or Fluent random row

16 Answered Questions

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

31 Answered Questions

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

  • 2008-12-10 15:59:51
  • MCS
  • 1131953 View
  • 1122 Score
  • 31 Answer
  • Tags:   mysql csv quotes

37 Answered Questions

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

12 Answered Questions

[SOLVED] MySQL case insensitive select

  • 2010-10-14 19:50:55
  • NoodleOfDeath
  • 256495 View
  • 227 Score
  • 12 Answer
  • Tags:   mysql select

24 Answered Questions

[SOLVED] How to reset AUTO_INCREMENT in MySQL?

6 Answered Questions

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

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
  • user9535105
  • 1638 View
  • 2 Score
  • 1 Answer
  • Tags:   mysql laravel

Sponsored Content