首页 > 解决方案 > 如何在 Laravel 5.8 中显示产品类别名称?

问题描述

我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。

我有这个架构:

Schema::create('product_selected_categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('category_id')->unsigned()->index();
            //$table->foreign('category_id')->references('id')->on('product_categories');
            $table->bigInteger('subcategory1_id')->default(0);
            $table->bigInteger('parent_subcategory1_id')->default(0);
            $table->bigInteger('subcategory2_id')->default(0);
            $table->bigInteger('parent_subcategory2_id')->default(0);
            $table->bigInteger('subcategory3_id')->default(0);
            $table->bigInteger('parent_subcategory3_id')->default(0);
            $table->bigInteger('subcategory4_id')->default(0);
            $table->bigInteger('parent_subcategory4_id')->default(0);
            $table->bigInteger('subcategory5_id')->default(0);
            $table->bigInteger('parent_subcategory5_id')->default(0);
            $table->bigInteger('subcategory6_id')->default(0);
            $table->bigInteger('parent_subcategory6_id')->default(0);
            $table->bigInteger('subcategory7_id')->default(0);
            $table->bigInteger('parent_subcategory7_id')->default(0);
            $table->bigInteger('subcategory8_id')->default(0);
            $table->bigInteger('parent_subcategory8_id')->default(0);
            $table->bigInteger('subcategory9_id')->default(0);
            $table->bigInteger('parent_subcategory9_id')->default(0);
            $table->bigInteger('product_id')->unsigned()->index();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');;
        });

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            //$table->bigInteger('category_id')->unsigned();
            //$table->foreign('category_id')->references('id')->on('product_categories');
            $table->smallInteger('id_delivery_vat')->unsigned();
            $table->foreign('id_delivery_vat')->references('id')->on('vat');
            $table->smallInteger('id_product_vat')->unsigned();
            $table->foreign('id_product_vat')->references('id')->on('vat');
            $table->bigInteger('id_producer')->unsigned();
            //$table->foreign('id_producer')->references('id')->on('product_producers');
            $table->string('name', 120)->nullable();
            $table->string('qr_code', 120)->nullable();
            $table->string('oe_code', 120)->nullable();
            $table->char('enable', 1)->default(0);
            $table->char('promo', 1)->default(0);
            $table->longText('description')->nullable();
            $table->decimal('product_price', 9, 2)->default(0);
            $table->decimal('promo_product_price', 9, 2)->default(0);
            $table->decimal('product_delivery_price', 9, 2)->default(0);
            $table->unsignedInteger('quantity')->default(0);
            $table->string('url_address', 160);
            $table->timestamps();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });
Schema::create('product_categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->char('enable', 1)->default(0);
            $table->string('name', 85)->nullable();
            $table->string('url_address', 160);
            $table->integer('level')->default(0);
            //$table->bigInteger('parent_id')->default(0);
            //$table->bigInteger('parent_id')->nullable();
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->foreign('parent_id')->references('id')->on('product_categories')->onDelete('cascade');
            $table->bigInteger('number')->default(0);
            $table->timestamps();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });

模型:

class ProductSelectedCategory extends Model
{
    protected $quarded = ['id'];
    protected $fillable = ['subcategory1_id', 'parent_subcategory1_id', 'category_id', 'subcategory2_id', 'parent_subcategory2_id', 'subcategory3_id', 'parent_subcategory3_id', 'subcategory4_id', 'parent_subcategory4_id', 'subcategory5_id', 'parent_subcategory5_id', 'subcategory6_id', 'parent_subcategory6_id', 'subcategory7_id', 'parent_subcategory7_id', 'subcategory8_id', 'parent_subcategory8_id', 'subcategory9_id', 'parent_subcategory9_id', 'product_id' ];
    public $timestamps = false;
}

class ProductCategory extends Model
{
    use scopeActiveTrait;

    protected $guarded = ['id'];
    protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'level', 'parent_id', 'number'];
    public $timestamps = true;
    //protected $table = 'products_category';

    public function parent()
    {
        return $this->belongsTo('App\ProductCategory', 'parent_id', 'id');
    }

    public function children()
    {
        return $this->hasMany('App\ProductCategory', 'id', 'parent_id');
    }

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

class Product extends Model
{
    use scopeActiveTrait;

    protected $fillable = ['company_id', 'id_delivery_vat', 'id_product_vat', 'id_producer', 'name', 'qr_code', 'oe_code', 'enable', 'promo', 'description', 'product_price', 'promo_product_price', 'product_delivery_price', 'quantity', 'url_address'];
    protected $quarded = ['id'];
    public $timestamps = true;

    public function category()
    {
        return $this->belongsTo('App\ProductCategory', 'id_producer');
    }

    public function producer()
    {
        return $this->belongsTo('App\ProductProducer', 'id_producer');
    }

    public function selected_categories()
    {
        return $this->hasMany('App\ProductSelectedCategory');
    }
}

我想显示带有类别名称的产品。category_id = category.id subcategory1_id = category.id .....等

在刀片中,我有:

@foreach($products as $product)
{{ $product->name }} - {{ $product->selected_categories->first()->parent_subcategory2_id }}, 
@endforeach

这返回我:产品名称 - 1(id 类别) - 3(id 类别)。

我需要名字 - 而不是 ID。我怎样才能改变它?

标签: phplaravellaravel-5

解决方案


我不明白为什么您的 product_selected_categories 表中填满了所有这些子类别。您正确使用了外键约束,但您不必在模型中将保护设置为“id”,因为如果未设置任何内容,则始终将其设置为“id”列。

此外,除非您有其他类型的类别,否则我会将 products_categories 表称为类别。

如果您只想与类别建立一对多关系,可以使用 Laravel 的内置功能(https://laravel.com/docs/5.8/eloquent-relationships#one-to-many)。目前,该数据库不是很好看。

然后,如果你想要一个类别的名称,只需输入

@foreach($products as $product)
{{ $product->name }} - {{ $product->category->name }}
@endforeach

如果您想保持目前的方式(我不推荐),您可以使用

@foreach($products as $product)
{{ $product->name }} - {{ $product->selected_categories->first()->name}}, 
@endforeach

推荐阅读