简体   繁体   English

如何在Laravel 5.4中用数据库填充选择框?

[英]how populate select box with database in Laravel 5.4?

I have an edit page where the data of a product is passed, on this page I have a select box for the category change, in it I need to insert all the categories registered in my database but only shows the category related to the product of the page. 我有一个编辑页面,用于传递产品数据,在此页面上,有一个用于类别更改的选择框,在其中需要插入数据库中注册的所有类别,但仅显示与以下产品相关的类别这一页。

Select Box of Edit view 选择编辑视图框

<select class="selectpicker" data-live-search="true">
          @foreach($produtos->categoria as $categoria)
          <option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
          @endforeach
          </select>

ProdutosController ProdutosController

public function edit($id)
    {
        $produtos = Produtos::find($id);

        return view('functions.edit')->with('produtos', $produtos);
    }

Routes 路线

Route::get('/product/update/{id}', 'ProdutosController@edit')->name("product::updateGet");
Route::post('/product/update/{id}', 'ProdutosController@update')->name("product::updatePost");

Any suggestions? 有什么建议么?

The problem is you are looping through the categories of that product, not all categories. 问题是您要遍历该产品的类别,而不是所有类别。 Here is a solution: 这是一个解决方案:

public function edit($id)
{
    $produtos = Produtos::find($id);
    $categories = Category::all();

    return view('functions.edit', compact('produtos', 'categories'));
}

then in your view: 然后在您看来:

<select class="selectpicker" data-live-search="true">
      @foreach($categories as $categoria)
          <option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
      @endforeach
</select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM