首页 > 解决方案 > Laravel 的新手使用关系模型

问题描述

我正在努力建立一个 Laravel API,我在模型关系方面遇到了问题,并且不确定我是否做得很好。

我的项目中有以下模型:

该模型代表电子商务上可用的产品,我试图通过以下方式将其关联起来:

1 个产品有 1 个品牌 + 1 个类别 + 1 个种类 + n 个 ProductImages(每个图像的一个产品 n 个图像只属于一个产品)+ n 个尺寸(我想为多个寄存器使用相同尺寸的表格行)。

我希望这与数据库相关,因此我可以从 1 个产品中获取所有相关信息,例如品牌或类别,但我还需要从一个品牌反向获取所有产品,例如。

所以我开始做以下模型功能:

牌:

public function product(){
        return $this->hasMany(Product::class);
    }

类别:

public function product(){
        return $this->hasMany(Product::class);
    }

种类:

public function product(){
        return $this->hasMany(Product::class);
    }

产品:

public function category(){
        return $this->belongsTo(Category::class);
    }

    public function brand(){
        return $this->belongsTo(Brand::class);
    }

    public function size(){
        $this->belongsTo(Size::class);
    }

    public function kind(){
        return $this->belongsTo(Kind::class);
    }

    public function productImages(){
        return $this->hasMany(ProductImages::class);
    }

尺寸:

public function product(){
        $this->belongsTo(Product::class);
    }

产品图片:

public function product(){
        $this->belongsTo(Product::class);
    }

如果我在 Tinker 上查询类别,我会得到类别列表,但如果我尝试获取属于同一类别的所有产品,我什么也得不到,但不确定它是否有权像我正在做的那样关联所有模型。

当我在 tinker 上查看产品品牌时,我得到的是:

    >>> $product = App\Models\Product::First();
=> App\Models\Product {#4307
     id: "1",
     descrption: "Voluptatibus et eius enim aut ipsa earum quis veritatis.",
     productImages_id: null,
     size_id: null,
     brand_id: "1",
     category_id: null,
     kind_id: "1",
     created_at: "2021-03-01 11:19:18",
     updated_at: "2021-03-02 10:26:14",
   }
>>> $product->brand();
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#4302}

迁移:

产品表:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('descrption');
            $table->integer('productImages_id')->nullable();
            $table->integer('size_id')->nullable();
            $table->integer('brand_id')->nullable();
            $table->integer('category_id')->nullable();
            $table->integer('kind_id')->nullable();
            $table->timestamps();
        });
    }

品牌表:

public function up()
    {
        Schema::create('brands', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

分类表:

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('category');
            $table->timestamps();
        });
    }

尺码表:

public function up()
    {
        Schema::create('sizes', function (Blueprint $table) {
            $table->id();
            $table->string('size');
            $table->integer('product_id');
            $table->timestamps();
        });
    }

种类表:

public function up()
    {
        Schema::create('kinds', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->string('kind');
            $table->timestamps();
        });
    }

产品图片表:

public function up()
    {
        Schema::create('product_images', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->string('url');
            $table->timestamps();
        });
    }

标签: phplaraveleloquentmodeltinker

解决方案


经过几次修改后,问题似乎来自迁移文件,因为引用外键的列被设置为整数以存储 id 并将其设置为 unsignedInteger 并重新运行迁移解决了该问题。这是 Products 表迁移文件:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('descrption');
            $table->unsignedInteger('size_id')->nullable();
            $table->unsignedInteger('brand_id')->nullable();
            $table->unsignedInteger('category_id')->nullable();
            $table->unsignedInteger('kind_id')->nullable();
            $table->timestamps();
        });
    }

推荐阅读