반응형
Model->where('id', Array)의 여러 조건을 실행할 수 있습니까?
제목만 봐도 알 수 있어요.
나는 이것을 할 수 있다:
DB::table('items')->where('something', 'value')->get()
그러나 다음과 같은 여러 값에 대해 where 조건을 확인하고 싶은 것은 다음과 같습니다.
DB::table('items')->where('something', 'array_of_value')->get()
쉽게 할 수 있는 방법이 있을까요?
다음과 같은 것이 있습니다.
$items = DB::table('items')->whereIn('id', [1, 2, 3])->get();
다음의 몇개의 솔루션을 사용할 수 있습니다.
$items = Item::whereIn('id', [1,2,..])->get();
또는 다음과 같이 입력합니다.
$items = DB::table('items')->whereIn('id',[1,2,..])->get();
여러 매개 변수가 필요한 경우:
$ids = [1,2,3,4];
$not_ids = [5,6,7,8];
DB::table('table')->whereIn('id', $ids)
->whereNotIn('id', $not_ids)
->where('status', 1)
->get();
사용할 수 있습니다.whereIn
배열을 두 번째 매개 변수로 받아들입니다.
DB:table('table')
->whereIn('column', [value, value, value])
->get()
여러 번 연결할 수 있습니다.
DB:table('table')->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->get();
이 방법은AND
교환입니다.필요하면OR
사용할 수 있습니다.orWhere
방법.
어드밴스드용where
진술들
DB::table('table')
->where('column', 'operator', 'value')
->orWhere(function($query)
{
$query->where('column', 'operator', 'value')
->where('column', 'operator', 'value');
})
->get();
ID로 검색하는 경우 다음을 사용할 수도 있습니다.
$items = Item::find(array_of_ids);
나는 id만 가져오고 배열로 검색하면 된다.
$shops = Shop::Where('user_id', Auth::id())->get('id');
$categories = Category::whereIn('shop_id',$shops)->paginate(7);
$whereData = [['name', 'test', ['id', '<>', '5'];
$users = DB::table('users')->where($whereData)->get();
위치 및 어레이 형식의 선택 조건 Larabel
use DB;
$conditions = array(
array('email', '=', 'user@gmail.com')
);
$selected = array('id','name','email','mobile','created');
$result = DB::table('users')->select($selected)->where($conditions)->get();
언급URL : https://stackoverflow.com/questions/30706603/can-i-do-model-whereid-array-multiple-where-conditions
반응형
'it-source' 카테고리의 다른 글
fork()의 목적은 무엇입니까? (0) | 2022.11.19 |
---|---|
ID 및 날짜를 기준으로 한 Row_number (0) | 2022.11.19 |
VueJ: Vue에서 계산된 "set/get"을 사용합니다.드래그 가능 및 VueX (0) | 2022.11.19 |
인덱스를 사용하지 않고 중복 삽입 방지 (0) | 2022.11.19 |
DOM 엘리먼트를 삭제했을 경우, 그 리스너도 메모리에서 삭제됩니까? (0) | 2022.11.19 |