拜托,我的控制器中有一个功能。
我想查询股票表以选择product_id
列表,在select
之后,它应该转到产品表以向用户显示产品的NAME
,而不是{{1} }。
product_id
我的PHP刀片有
public function getStockList()
{
$stock = DB::table("stocks")->pluck("product_id","product_id")->all();
$products = DB::table("products")->pluck("name")->where('id', $stock);
return view('shop.shop')->with(['stocks' => $stocks,
'stock' => $stock, 'products' => $products]);
}
基本上,我不希望向用户显示{!! Form::select
(
'stock',
['' => 'Select'] +$stock,'',
array(
'class'=>'form-control',
'id'=>'country',
'style'=>'width:350px;'
)
)!!}
{!! Form::select
(
'product_id',
[''=>'Select Supplier'] + $products, null,
['class'=>'form-control']
)!!}
,而是显示产品名称。
答案 0 :(得分:1)
试试这个
public function getStockList()
{
$stock = DB::table("stocks")->pluck("product_id");
$products = DB::table("products")->whereIn('id', $stock)->pluck("name","id");
return view('shop.shop')->with([
'stocks' => $stocks,
'stock' => $stock,
'products' => $products
]);
}
使用以下
替换产品列表选择框{!! Form::select('product_id',$products,null, ['class'=>'form-control','placeholder'=>'Select Products'])!!}
答案 1 :(得分:0)
你需要数组传递形式
/* $product_array = ['1' => 'product 1', '2' => 'product 2', '3' => 'product 3']; */
所以在你的控制器中
$product_array = []
foreach($products as $product){
$product_array[$product->id] = $product->name;
}
return view('shop.shop')->with(['stocks' => $stocks,
'stock' => $stock, 'products' => $products , 'product_array'=>$product_array]);
在你看来
{{!! Form::select('product_id', $product_array , null, ['class'=>'form-control'])!! }}
希望这会有所帮助。