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

使用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条回答 默认 最新

  • douzhangkui2467 2017-07-19 11:34
    已采纳

    Try this

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

    Replace your product list select box with the following

    {!! Form::select('product_id',$products,null, ['class'=>'form-control','placeholder'=>'Select Products'])!!}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doubi4435 2017-07-19 11:50

    you need array to pass in form

    /* $product_array = ['1' => 'product 1',  '2' => 'product 2', '3' => 'product 3']; */
    

    so in your controller

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

    And in your view

    {{!! Form::select('product_id', $product_array , null, ['class'=>'form-control'])!! }}
    

    hope this will help.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题