如何在laravel中使用关系?

时间:2018-03-21 03:08:51

标签: laravel laravel-5 relationship

我在laravel 5.6中使用了关系。

我使用迁移创建product表:

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('slug');
        $table->text('description');
        $table->string('tags');
        $table->string('original_price');
        $table->integer('view_order')->default(0);
        $table->unsignedInteger('admin_id');
        $table->foreign('admin_id')->references('id')->on('admins');
        $table->boolean('status');
        $table->timestamps();
    });

我用迁移创建category表:

Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->boolean('status');
        $table->timestamps();
    });

使用迁移创建product_categories表:

Schema::create('product_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products');
        $table->unsignedInteger('category_id');
        $table->foreign('category_id')->references('id')->on('categories');
        $table->timestamps();
    });

现在,我将Bootstrap Multiselect用于一个产品中的类别。

category模型中:

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function products()
{
    return $this->belongsToMany(Product::class);
}

Product模型中:

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function categories()
{
    return $this->belongsToMany(Category::class);
}

如何在category_id表格中添加product_idproduct_categories

4 个答案:

答案 0 :(得分:1)

检查与Many to many Relationships.

相关的文档

您的数据透视表不遵循Laravel的惯例,更新您的表名或更新您的关系以解决此问题。

约定是两个模型的字母顺序,因此您的数据透视表应命名为:category_product

如果您不想更新表名,请更新您的关系。

public function categories()
{
    return $this->belongsToMany(Product::class, 'product_categories')
}

public function products()
{
    return $this->belongsToMany(Category::class, 'product_categories')
}

现在“将条目保存到数据透视表” - 或换句话说:创建两个模型之间的关系 - 您可以使用attachsync方法

$product->categories()->attach($category);
$product->categories()->attach([$categoryId1, $categoryId2]);

sync不同。

  

sync方法接受要放在中间表上的ID数组。任何不在给定数组中的ID都将从中间表中删除。

要分离(删除数据透视表中的条目),只需使用detach方法。

$product->categories()->detach([1, 2]);

当然,对Category执行相同操作。

答案 1 :(得分:0)

您只需添加数据透视表:

public function products()
{
    return $this->belongsToMany(Product::class, 'product_categories');
}

public function categories()
{
    return $this->belongsToMany(Category::class, 'product_categories');
}

答案 2 :(得分:0)

默认情况下,laravel从相关模型名称的字母顺序派生表名。此处您的模型名称为ProductCategory,派生的关系表将为category_product,因为类别按字母顺序排在产品之前。要么你可以更改表名,要么可以覆盖这个我提到表名作为关系方法中的第二个参数,如下所示。 公共功能产品()

{
    return $this->belongsToMany(Product::class, 'product_categories');
}

public function categories()
{
    return $this->belongsToMany(Category::class, 'product_categories');
}

答案 3 :(得分:0)

您的模型名称为ProductCategory,派生的关系表格为category_product,因为类别按字母顺序排在产品之前。

您只需添加数据透视表:

public function categories()
{
    return $this->belongsToMany(Product::class, 'product_categories')
}

public function products()
{
    return $this->belongsToMany(Category::class, 'product_categories')
}

现在保存关系:

$product->categories()->attach($category);
$product->categories()->attach([$category_id_1, $category_id_2]);