du4373
2017-07-19 11:28
浏览 200
已采纳

使用Laravel Eloquent中另一个表的ID显示表的名称

Please, I have a function in my controller. I want to query the stock table to select the list of product_id and after select, it should go to product table to display the NAME of product to the user instead of product_id.

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]);
}

My PHP blade has

{!! 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']
)!!}

Basically, I don't want product_id to be displayed to the user but the product name.

图片转代码服务由CSDN问答提供 功能建议

请在我的控制器中有一个功能。 我想查询stock表以选择< 代码> product_id ,在选择之后,应该转到产品表,向用户显示产品的 NAME ,而不是 product_id

  public function getStockList()
 {
 $ stock = DB :: table(“stocks”) - &gt; pluck(“product_id”,“product_id”) -  &gt; all(); 
 $ products = DB :: table(“products”) - &gt; pluck(“name”) - &gt; where('id',$ stock); 
返回视图('shop。  shop') - &gt; with(['stocks'=&gt; $ stocks,
'stock'=&gt; $ stock,'products'=&gt; $ products]); 
} 
  <  / pre> 
 
 

我的PHP刀片已

  {!!  Form :: select 
(
'stock',
 [''=&gt;'选择'] + $ stock,'',
数组(
'class'=&gt;'form-control',  
'id'=&gt;'country',
'style'=&gt;'width:350px;'
)
)!!} 
 
 {!!  Form :: select 
(
'product_id',
 [''=&gt;'选择供应商'] + $ products,null,
 ['class'=&gt;'form-control'] 
)  !!} 
   
 
 

基本上,我不希望 product_id 显示给用户,而是产品名称。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题