1

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?

2
  • Grab all of the categories, assign it to another variable, and pass that in as well. Then use that for the select instead. Commented Apr 17, 2017 at 13:55
  • Fetch using $categories = Categoria::all() and pass ->with('categories ', $categories ) then use that in view. Commented Apr 17, 2017 at 13:58

1 Answer 1

2

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> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.