where NotIn不工作或者我的代码错误



目前,我正在将商品添加到价目表中,但不应允许两次添加相同的商品,特别是相同的ID。

我使用whereNotIn子句来实现这一点,但它不起作用。有人能告诉我我的代码出了什么问题吗?谢谢

public function findArticulo(Request $request)
{
$id = $request->id;
$articulos = Articulo::query()->select([
'id',
'codigo_interno',
'codigo_comercial',
'impuestos_venta',
'impuestos_compra',
'nombre',
'descripcion',
'status'])
->where('status','activo')->whereNotIn('id', function($query)use($id){
$query->select('articulo_id')
->from('listas_precios_articulos')
->where('lista_precio_id', $id)
->whereNotIn('estatus', ['eliminado']);
})
->where('nombre', 'like', "%".$request->nombre."%")
->Orwhere('codigo_interno', 'like', "%".$request->nombre."%")
->Orwhere('codigo_comercial' , 'like' , "%".$request->nombre."%")
->get()
->toArray();
return response()->json($articulos,200);
}

此查询必须返回一个数组以使其在where NotIn语句中正确

$query->select('articulo_id')
->from('listas_precios_articulos')
->where('lista_precio_id', $id)
->whereNotIn('estatus', ['eliminado'])
->toArray();

也许这是一个打字错误->Orwhere。。。必须是->或何处

更新你能拆分查询并这样做吗:

$id = $request->id;
$pluckedIds = ListasPreciosArticulo::
where('lista_precio_id', $id)
->where('estatus','!=','eliminado')
->pluck('id');
$articulos = Articulo::query()->select([
'id',
'codigo_interno',
'codigo_comercial',
'impuestos_venta',
'impuestos_compra',
'nombre',
'descripcion',
'status'])
->where('status','activo')->whereNotIn('id', [$pluckedIds])
->where('nombre', 'like', "%".$request->nombre."%")
->orWhere('codigo_interno', 'like', "%".$request->nombre."%")
->orWhere('codigo_comercial' , 'like' , "%".$request->nombre."%")
->get();
return response()->json($articulos,200);

最新更新