Insert data into a Pivo Laravel table

Asked

Viewed 75 times

0

I have a many to many relationship between items and area, as I do to relate the two within my item_area table in my store method?

This is my view

<h3>Criar um novo Item</h3>
<form class="form-horizontal" method="post" action="#">
    <label for="name">Nome</label><br>
    <input type="text" name="name"><br>
    <label for="area-select">Selecione a Area</label><br>
    <select name="area-select">
        @forelse ($areas as $area)
            <option value="{{ $area->id }}">{{$area->name}}</option>
        @empty
            <option>Nenhuma Area a ser selecionada</option>
        @endforelse
    </select>
    <br><br>    
    <button type="submit">Cadastrar Produto</button>
</form>

This is my create method

public function create()
{
    $areas = Area::all();

    return view('item.create')->with('areas', $areas);
}

My doubt is how to recover the area id on the route and how to relate both in the store method that will store both within the database?

Would something like this?

public function store(Request $request)
{
    $select_area = $request->input('id');
    $item->area = $select_area;
    ...
}
  • To recover the last saved id use DB::getPDO()->Lastinsertid();

  • Put the two models in your question and also put whole view

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.