【问题标题】:I need to convert that query in laravel Query我需要在 laravel Query 中转换该查询
【发布时间】:2018-04-23 16:08:13
【问题描述】:
SELECT * 
  FROM `events` 
 WHERE (`customer_bookable` = 1 and template = 1 ) 
    OR (`customer_bookable` = 0 and template = 0 ) 
   and (`start_date_time` BETWEEN '2017-09-01 00:00:00' and '2017-09-21 00:00:00') 
   And (profile_id = 10) 
   and (event_live_status = 1) 
   and (class != 0) 
   and (privacy_status = 1)

【问题讨论】:

  • 您好!你自己试过什么?我们可以帮助您(调整)您的代码,但我们不会为您编写代码。
  • 这是手册laravel.com/docs/5.5/queries,介绍如何将 SQL 查询转换为 Laraval 查询生成器代码。

标签: mysql laravel


【解决方案1】:

您需要“或”部分的帮助吗?您可以在 SQL 中使用括号的地方使用闭包:

...
->where( function ($query) {
   $query->where('customer_bookable',1)
         ->where('template',1);
 })->orWhere( function ($query) {
   $query->where('customer_bookable',0)
         ->where('template',0);
 })
 ...

【讨论】:

  • 能否请您发布您的代码,以便我们查看它在哪里不起作用?
【解决方案2】:

您的查询可能如下所示,

$events = DB::('events')
    ->where( function ($query) {
         $query->where('customer_bookable',1)
               ->where('template',1)
               ->orWhere('customer_bookable',0)
               ->orWhere('template',0);
    })
    ->where( function ($query) {
          $query->whereBetween('start_date_time',['2017-09-01 00:00:00' and '2017-09-21 00:00:00'])
          ->where('profile_id',10)     
          ->where('event_live_status',1)
          ->where('class','!=',0)
          ->where('privacy_status',1);
     });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2021-07-27
    • 2021-08-01
    • 1970-01-01
    • 2016-04-27
    • 2021-05-22
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多