Laravel Eloquent orWhere Query

后端 未结 4 1553
悲哀的现实
悲哀的现实 2021-01-17 18:05

Can someone show me how to write this query in Eloquent?

SELECT * FROM `projects` WHERE `id`=\'17\' OR `id`=\'19\'

I am thinking

         


        
4条回答
  •  孤街浪徒
    2021-01-17 18:50

    You could do in three ways. Assume you've an array in the form

    ['myselect' => [11, 15, 17, 19], 'otherfield' => 'test', '_token' => 'jahduwlsbw91ihp'] which could be a dump of \Input::all();

    1.    Project::where(function ($query) {
            foreach(\Input::get('myselect') as $select) {
               $query->orWhere('id', '=', $select);
            }
         })->get();
      
    2.    Project::whereIn('id', \Input::get('myselect'))->get();
      
    3.    $sql = \DB::table('projects');
         foreach (\Input::get('myselect') as $select) {
             $sql->orWhere('id', '=', $select);
         }
         $result = $sql->get();
      

提交回复
热议问题