12

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.

2 Answers 2

28

You can use whereRaw like as

DB::table('stories')->whereRaw("find_in_set('4',author)")->get();
Sign up to request clarification or add additional context in comments.

Comments

3

First convert your string to array using

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

2 Comments

Why to convert string into array if you can simply use find_in_set
@uchicha you are right..but different human has different mind and different mind has different way to logic... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.