php - Update value to database with many to many relations Laravel -


i want update value data radio button database table product_categories i'm confused. because data belongs many.

here belongs many function in product model

public function categories()      {          return $this->belongstomany(category::class,'products_categories','product_id','category_id')              ->withpivot('id')              ->orderby('category_id')              ->withtimestamps();      }

here radio button structure in view

{!! form::model($product, ['route' => ['products.update', $product->id], 'method' => 'patch']) !!}  <div class="dropdown dropdown-full-width dropdown-category">    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">            <span class="name">                <span id="category-select">choose category</span>            </span>            <span class="caret"></span>        </button>    <div class="dropdown-menu row">      <div class="col-md-4">        <li><strong>by {{ $category[1] }}</strong></li>        @foreach($category1 $occasions)        <li>          <div class="checkbox">            <label><input type="radio" name="category['occasions']" class="category-radio" value="{{ $occasions->id }}"> {{ $occasions->name }}</label>          </div>        </li>        @endforeach      </div>            <div class="col-md-4">        <li><strong>by {{ $category[2] }}</strong></li>        @foreach($category2 $types)        <li>          <div class="checkbox">            <label><input type="radio" name="category['types']" class="category-radio" value="{{ $types->id }}"> {{ $types->name }}</label>          </div>        </li>        @endforeach      </div>        <div class="col-md-4">        <li><strong>by {{ $category[3] }}</strong></li>        @foreach($category3 $flowers)        <li>          <div class="checkbox">            <label><input type="radio" name="category['flowers']" class="category-radio" value="{{ $flowers->id }}"> {{ $flowers->name }}</label>          </div>        </li>        @endforeach      </div>    </div>  </div>  {!! form::close() !!}

and here update function in controller

public function update($id, updateproductrequest $request)      {          $product = $this->productrepository->findwithoutfail($id);            $categories = product::with('categories')->where('id','=', $product->id)->get();          $idcategories = $categories[0]->categories;                    if (empty($product)) {              flash::error('product not found');                return redirect(route('products.index'));          }          //place update code radio button here//            flash::success('product updated successfully.');            return redirect(route('products.index'));      }

i'm confused many many relation sync.

update

here table structure in product_categories

enter image description here


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -