2017-12-06 10 views
2

は、私は私のdatabase.Inにuser_webhookテーブルからテンプレートを取得したいWHERE条件私はUSER_ID、APP_IDをチェックし、いずれかnotify_adminまたはnotify_customer値は、クエリを使用していますuser_webhookのtable.I中1である場合は、am ...Laravelモデルここでの条件を使用するか?

$templates= $this->where('notify_admin',1) 
       ->orwhere('notify_customer',1) 
       ->where('user_webhooks.user_id',$user_id) 
       ->where('user_webhooks.app_id',$app_id) 
       ->select( 'webhooks.id as webhook_id','webhooks.app_id','webhooks.topic','webhooks.type','webhooks.action', 
          'webhooks.sms_template','user_webhooks.id','user_webhooks.notify_admin', 
          'user_webhooks.notify_customer','user_webhooks.user_id','user_webhooks.sms_template_status', 
          'user_webhooks.sms_template as sms' 
         ) 
       ->join ('webhooks',function($join){ 
         $join>on('webhooks.id','=','user_webhooks.webhook_id'); 
         }) 
       ->get() 
       ->toArray(); 

私はDB :: getQueryLog()を使用してクエリを取得するとき、私は、クエリが

select `telhok_webhooks`.`id` as `webhook_id`, `telhok_webhooks`.`app_id`, 
`telhok_webhooks`.`topic`, `telhok_webhooks`.`type`, `telhok_webhooks`.`action`, 
`telhok_webhooks`.`sms_template`, `telhok_user_webhooks`.`id`, 
`telhok_user_webhooks`.`notify_admin`, `telhok_user_webhooks`.`notify_customer`, 
`telhok_user_webhooks`.`user_id`, `telhok_user_webhooks`.`sms_template_status`, 
`telhok_user_webhooks`.`sms_template` as `sms` from `telhok_user_webhooks` 

inner join 
`telhok_webhooks` on `telhok_webhooks`.`id` = `telhok_user_webhooks`.`webhook_id` 

    where `notify_admin` = ? or `notify_customer` = ? and `telhok_user_webhooks`.`user_id` 
    = ? and `telhok_user_webhooks`.`app_id` = ? 

すべてAPP_IDとUSER_IDの結果を与えるクエリの結果のように思えるが分かりました。 だからどこでORの使用を教えてください。 ありがとうございます。

答えて

0

制約をどこで連鎖させてもよいし、クエリに追加したり、句を付けたりすることもできます。

$users = DB::table('users') 
       ->where('votes', '>', 100) 
       ->orWhere('name', 'John') 
       ->get(); 

高度な使用法:

$users = DB::table('users') 
        ->whereIn('id', [1, 2, 3]) 
        ->get(); 

Usere::where('id', 46) 
     ->where('id', 2) 
     ->where(function($q) { 
      $q->where('Cab', 2) 
      ->orWhere('Cab', 4); 
     }) 
     ->get(); 

方法は、与えられた列の値が指定された配列内に含まれることを確認する、請求ザorWhere方法は、ここでの方法と同じ引数を受け付け

詳細:https://laravel.com/docs/5.5/queries

+0

おかげ卿の例が有用であるこれらの2つの列を比較するのではなく、クエリ内の他のすべてのwheresを比較します。 – ShaanSetia

+0

私はstackoverflowの評判が低い、今私はあなたに報酬として感謝を与えることができます。 – ShaanSetia

+0

それはまた、指摘するには15の評判が必要です。 – ShaanSetia

1

変更

->where('notify_admin',1) 
->orwhere('notify_customer',1) 

このなし

->where(function($q){ 
    $q->where('notify_admin',1) 
    ->orWhere('notify_customer',1); 
}) 

に、orWhereはちょうど

+0

ありがとうございます。例が便利です – ShaanSetia

関連する問題