Vinícius
Vinícius

Reputation: 483

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

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?

Upvotes: 1

Views: 1500

Answers (1)

Farzin Farzanehnia
Farzin Farzanehnia

Reputation: 1060

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> 

Upvotes: 2

Related Questions