[英]how to show all the products laravel
I have a query where I show all the products that are in that warehouse, but if it has more than one it shows me the x quantity but with the same data, thats because I have ->first();
我有一个查询,其中我显示了该仓库中的所有产品,但如果它有多个产品,它会显示 x 数量但具有相同的数据,那是因为我有
->first();
but If I remove ->first();
但是如果我删除
->first();
it says an error whit the traceability
.它说
traceability
出错。
$Input = $request->key;
$almacen = $request->almacen;
$name = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
->whereRaw("(products.reference LIKE '%$Input%' OR products.name LIKE '%$Input%') AND products.deleted_at is null")
->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->where('warehouses.id', $almacen)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability',
'inventories.warehouse_id'
)
->get();
$array = [];
foreach ($name as $key) {
//$key->traceability
array_push($array, $key->reference);
}
//$array = array_unique($array);
$html = '';
if ($name != '[]') {
foreach ($array as $value) {
$prodName = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
->where('products.reference', $value)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability'
)
->first();
//return $value;
$html .= '<div><a style="color: #000000" class="suggest-element" traceability="'.$prodName->traceability.'" reference="' . $value . '" sku="' . $prodName->sku . '" name="' . $prodName->name . '" lot="' . $prodName->lot . '" expirationDate="' . $prodName->expirationDate . '" data="' . ($value) . " " . ($prodName->name) . '" id="' . $prodName->id . '">' . ($value) . " " . ($prodName->name) ." " . ($prodName->lot) ." " . ($prodName->expirationDate) ." " . ($prodName->traceability) .'</a></div>';
}
} else {
$html .= '<div><a style="color: #000000" class="suggest-element" exist="0" data="Sin coincidencias." id="0">Sin coincidencias.</a></div>';
}
return $html;
});` });`
I would like to see the products whit the correct data,我想查看具有正确数据的产品,
Your code is currently only returning the first row in the database because you are using first()
.您的代码目前仅返回数据库中的第一行,因为您使用的是
first()
。
If you remove first()
, you will just be left with a Query Builder
and will get an error like " Undefined property: Illuminate\Database\Query\Builder::$traceability
" - because you haven't got the data yet.如果您删除
first()
,您将只剩下一个Query Builder
,并会收到类似“ Undefined property: Illuminate\Database\Query\Builder::$traceability
”的错误 - 因为您还没有获得数据。
You can use get()
instead of first()
to get all of the rows, and this will return a Collection
.您可以使用
get()
而不是first()
来获取所有行,这将返回一个Collection
。
You can also use ->whereIn('products.reference', $array)
instead of looping through all of the elements in $array
您还可以使用
->whereIn('products.reference', $array)
而不是遍历 $ $array
中的所有元素
You are also comparing $name
to the string '[]'
instead of an empty array.您还将
$name
与字符串'[]'
而不是空数组进行比较。 You can use empty()
instead: if (!empty($name)) {
您可以改用
empty()
: if (!empty($name)) {
And then you can simply loop through the Collection
and add the html:然后您可以简单地遍历
Collection
并添加 html:
// Use empty() - This checks if an array is empty or not
if (!empty($name)) {
$products = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
// Use whereIn - This checks if any of the values in $array match products.reference
->whereIn('products.reference', $array)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability'
)
// Return all rows
->get();
foreach ($products as $product) {
$html .= '<div><a style="color: #000000" class="suggest-element" traceability="' . $product->traceability . '" reference="' . $product->reference . '" sku="' . $product->sku . '" name="' . $product->name . '" lot="' . $product->lot . '" expirationDate="' . $product->->expirationDate . '" data="' . ($product->reference) . " " . ($product->->name) . '" id="' . $product->->id . '">' . ($product->reference) . " " . ($product->name) . " " . ($product->lot) . " " . ($product->expirationDate) . " " . ($product->traceability) . '</a></div>';
}
} else {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.