Search Posts

How to save sub attribute in 3rd table in laravel

I want to save my sub-attribute data in 3rd table but i got Call to undefined method AppProduct::mores() error.

Logic:

Getting product_id + getting submore_id save it to more_product table.

The issue:

The issue is that I don’t want to get more_id but instead I want to get submore_id.

hierarchy

  1. More = CPU
  2. Submore = Core i7
  3. Product_id = 67

Result will be like:

enter image description here

Normally i should save attribute id and that would be easy with something like this:

$product->mores()->sync($request->mores, false);

after saving product BUT as I need to gave submore id I get error and it says:

Call to undefined method AppProduct::mores()

which lead me to:

$product->mores()->sync($request->mores, false);

Codes

create method:

public function create()
    {
      $categories = Category::all();
      $subcategories = Subcategory::all();
      $submores = Submore::all();
      $user = Auth::user();
      $brands = Brand::all();
      return view('admin.products.create', compact('user', 'categories', 'subcategories', 'submores', 'brands'));
    }

Store method:

//......
      $product->save();
      $product->mores()->sync($request->mores, false);

      //Display a successful message upon save
      Session::flash('flash_message', 'Product, '. $product->title.' created');
      return redirect()->route('products.index');

Create blade page:

  <label for="mores">Attributes</label>
  <select class="tagsselector form-control" name="mores[]" multiple="multiple">
  <option>Select Attribute</option>
    @foreach($submores as $more)
       <option value="{{ $more->id }}">{{ $more->title }}</option>
    @endforeach
  </select>

Any idea?

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *