laravel 如何在laravel中使用逗号分隔值的列上使用'where'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33493588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use 'where' on column with comma separated values in laravel
提问by AnkitJ
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.
我正在使用带有 Laravel 框架的 php。我想从包含用户 ID 的表中获取行。这里的问题是用户 id 是整数值,而 column(author) 包含多个逗号分隔的整数值。这是示例。
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 行:2,4,5 | 第 2 行:1,3,14 | 第 3 行:4,5,我会得到第 1 行和第 3 行。所以,请帮我找到正确的行。
回答by Narendrasingh Sisodia
You can use whereRaw
like as
你可以whereRaw
像这样使用
DB::table('stories')->whereRaw("find_in_set('4',author)")->get();
回答by PRANAV
First convert your string
to array
using
首先将您转换string
为array
使用
$row = "2,4,5";
$idsArr = explode(',',$row);
DB::table('stories')->whereIn('author',$idsArr)->get();