Laravel Eloquent orWhere Query

后端 未结 4 1595
悲哀的现实
悲哀的现实 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:42

    The best approach for this case is using Laravel's equivalent for SQL's IN().

    Project::whereIn('id', [17, 19])->get();
    

    Will be the same as:

    SELECT * FROM projects WHERE id IN (17, 19)
    

    This approach is nicer and also more efficient - according to the Mysql Manual, if all values are constants, IN sorts the list and then uses a binary search.

提交回复
热议问题