php - How to save sub attribute in 3rd table in laravel
I want to save mysub-attribute
data in3rd
table but i gotCall to undefined method App\Product::mores()
error.
Logic:
Gettingproduct_id
+ gettingsubmore_id
save it toproduct_mores
table.
The issue:
The issue is that I don't want to getmore_id
but instead I want to getsubmore_id
.
hierarchy
- More = CPU
- Submore = Core i7
- Product_id = 67
Result will be like:
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 gavesubmore
id I get error and it says:
Call to undefined method App\Product::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?
Answer
Solution:
SOLVED
All I needed was to add table and column names in my models functions like so:
And also change my save function from
mores
tosubmores
like so:Then change the
name
andfor
in my form tosubmores