简体   繁体   中英

Laravel: whereIn with variable

I'm trying to collect all the records that belong to the sections that happen to them in the variable $sections. But only those from section 1 pick me up. Any suggestions?

$sections = '1,2,3';

$data = News::where('active', '1')
        ->whereIn('section_id', [$sections])
        ->get();

If I substitute $sections in the query for the values, this works, but if I use the variable $sections, It doesn't work.

Thanks.

当您使用whereIn()您必须传递一个数组而不是字符串:

$sections = explode(',', '1,2,3');

Your $sections variable is a string. And you are passing that string to the array, not each individual numbers in the string.

Ex:

$users = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM