How to use 'where' on column with comma separated values in laravel

倖福魔咒の 提交于 2021-02-18 11:20:07

问题


I am using php with laravel framework. I want to get rows from table that contains user id. Problem here is that user id is integer value and column(author) contain more than one comma separated integer values. Here is example.

DB::table('stories')->where('author','4')->get();

author column have values : row 1: 2,4,5 | row 2: 1,3,14 | row 3 : 4,5 and I will get row 1 and 3. So, Please help me to get right rows.


回答1:


You can use whereRaw like as

DB::table('stories')->whereRaw("find_in_set('4',author)")->get();



回答2:


First convert your string to array using

$row = "2,4,5";
$idsArr = explode(',',$row);  
DB::table('stories')->whereIn('author',$idsArr)->get();


来源:https://stackoverflow.com/questions/33493588/how-to-use-where-on-column-with-comma-separated-values-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!