[英]How to add multiple category in product laravel
I need to store my product in multiple categories.我需要将我的产品存储在多个类别中。 ie I can choose multiple categories and the items will be stored under these all categories.
即我可以选择多个类别,项目将存储在这些所有类别下。
I am confused about how can i achieve that.我对如何实现这一目标感到困惑。 I think i have two options
我想我有两个选择
please help me how can I do that请帮助我我该怎么做
i tried by writing but didn't worked我尝试通过写作但没有奏效
foreach($response->category_id as $category_id){
$product->category_id = $category_id;
$product-save();
}
Form for that表格
<div class="form-group row" id="category">
<label class="col-md-3 col-from-label">{{translate('Category')}} <span class="text-danger">*</span></label>
<div class="col-md-8">
<select multiple class="form-control aiz-selectpicker" name="category_id" id="category_id" data-live-search="true" required>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->getTranslation('name') }}</option>
@foreach ($category->childrenCategories as $childCategory)
@include('categories.child_category', ['child_category' => $childCategory])
@endforeach
@endforeach
</select>
</div>
</div>
Controller控制器
public function store(Request $request)
{
$product = new Product;
$product->name = $request->name;
$product->added_by = $request->added_by;
$product->category_id = $request->category_id;
$product->brand_id = $request->brand_id;
$product->barcode = $request->barcode;
$product->save();
//VAT & Tax
if($request->tax_id) {
foreach ($request->tax_id as $key => $val) {
$product_tax = new ProductTax;
$product_tax->tax_id = $val;
$product_tax->product_id = $product->id;
$product_tax->tax = $request->tax[$key];
$product_tax->tax_type = $request->tax_type[$key];
$product_tax->save();
}
}
//Flash Deal
if($request->flash_deal_id) {
$flash_deal_product = new FlashDealProduct;
$flash_deal_product->flash_deal_id = $request->flash_deal_id;
$flash_deal_product->product_id = $product->id;
$flash_deal_product->save();
}
enter image description here
//combinations start
$options = array();
if($request->has('colors_active') && $request->has('colors') && count($request->colors) > 0) {
$colors_active = 1;
array_push($options, $request->colors);
}
if($request->has('choice_no')){
foreach ($request->choice_no as $key => $no) {
$name = 'choice_options_'.$no;
$data = array();
foreach ($request[$name] as $key => $eachValue) {
array_push($data, $eachValue);
}
array_push($options, $data);
}
}
$product->save();
flash(translate('Product has been inserted successfully'))->success();
Artisan::call('view:clear');
Artisan::call('cache:clear');
}
}
You probaby also have a categories
table with something like [id,title]
columns.您可能还有一个
categories
表,其中包含[id,title]
列之类的内容。 If you can have multiple products in a category, and categories for each product, then it's a Many-To-Many relationship.如果您可以在一个类别中拥有多个产品,并且每个产品都有多个类别,那么这就是多对多关系。 (See the laravel docs at https://laravel.com/docs/8.x/eloquent-relationships#many-to-many ).
(请参阅https://laravel.com/docs/8.x/eloquent-relationships#many-to-many 上的 laravel 文档)。
Add a pivot table in your database named category_product
with columns [product_id, category_id]
.在名为
category_product
的数据库中添加一个包含[product_id, category_id]
列的数据透视表。 You can create a migration to do this (See https://laravel.com/docs/8.x/migrations#generating-migrations on how to do this).您可以创建迁移来执行此操作(有关如何执行此操作,请参阅https://laravel.com/docs/8.x/migrations#generating-migrations )。
Add the relationships in your Product
and Category
models.在您的
Product
和Category
模型中添加关系。 In your product model,在您的产品型号中,
public function categories() { return $this->belongsToMany(Category::class);公共函数类别(){ 返回 $this->belongsToMany(Category::class); }
}
And in your category model:在您的类别模型中:
public function products()
{
return $this->belongsToMany(Product::class);
}
Note: If you name your pivot table anything other than category_product
(alphabetical) then you can pass the table name as a second parameter.注意:如果您将数据透视表命名为
category_product
(字母顺序)以外的任何名称,那么您可以将表名称作为第二个参数传递。 eg $this->belongsToMany(Product::class, 'mypivottable');
例如
$this->belongsToMany(Product::class, 'mypivottable');
This table maps any products to any categories.此表将任何产品映射到任何类别。 If product with ID 42 has categories with IDs 3 and 4, then you'll have 2 entries in your pivot table: [product_id=42, category_id=3] and [product_id=42, category_id=4].
如果 ID 为 42 的产品具有 ID 为 3 和 4 的类别,那么您的数据透视表中将有 2 个条目:[product_id=42, category_id=3] 和 [product_id=42, category_id=4]。
Laravel will handle pretty much everything else automatically. Laravel 会自动处理几乎所有其他事情。 If you call
$product->categories()
then it will return an Eloquent Builder
to query further on the categories that $product
is attached to.如果你调用
$product->categories()
那么它会返回一个 Eloquent Builder
来进一步查询$product
附加到的类别。 If you use $product->categories
as a 'magic' attribute, it will return a Collection
of categories that had their ids in a row matching the product_id
.如果您使用
$product->categories
作为 'magic' 属性,它将返回一个类别的Collection
,这些类别的 id 在一行中与product_id
匹配。 The reverse will work for a category.反向将适用于一个类别。 To get a
Collection
of products in a $category
, just use $category->products
, or call $category->products()->where(...)
to query further on the products in the pivot table that have a row with the matching category_id
.要获取
$category
的产品Collection
,只需使用$category->products
,或调用$category->products()->where(...)
进一步查询数据透视表中具有一行的产品具有匹配的category_id
。
To save a new product,要保存新产品,
$product = new Product(); $product = 新产品(); // ... $product->save();
// ... $product->save();
$product->categories()->attach( $arrayOfCategoryIds ); $product->categories()->attach( $arrayOfCategoryIds ); // This last line saves the rows in the pivot table
// 最后一行保存数据透视表中的行
There are more methods listed in the docs for detaching or updating the relationship on https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships文档中列出了更多用于在https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships上分离或更新关系的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.