【问题标题】:How to use relationship in laravel?如何在 laravel 中使用关系?
【发布时间】:2018-03-21 03:08:50
【问题描述】:

我在 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);
}

如何在product_categories表中添加category_idproduct_id有关系?

【问题讨论】:

  • 在stackoverflow中提问之前,请确保您已经完成了足够的研究。

标签: laravel laravel-5 relationship


【解决方案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 方法。 p>

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

sync 不同。

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

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

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

当然,Category 也一样。

【讨论】:

    【解决方案2】:

    您的模型名称是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]);
    

    【讨论】:

      【解决方案3】:

      您只需添加数据透视表:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2020-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-17
          • 2022-01-25
          • 1970-01-01
          • 2018-04-29
          相关资源
          最近更新 更多