Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 982
AnkitJ Avatar asked Nov 03 '15 07:11

AnkitJ


2 Answers

You can use whereRaw like as

DB::table('stories')->whereRaw("find_in_set('4',author)")->get();
like image 96
Narendrasingh Sisodia Avatar answered Sep 18 '22 09:09

Narendrasingh Sisodia


First convert your string to array using

$row = "2,4,5";
$idsArr = explode(',',$row);  
DB::table('stories')->whereIn('author',$idsArr)->get();
like image 33
PRANAV Avatar answered Sep 20 '22 09:09

PRANAV