显示基于Laravel 5.8中选定类别的产品?

时间:2019-10-07 20:58:36

标签: php laravel laravel-5

以下代码是我的类别(product_categories),产品列表(products)和分配给产品的类别(product_selected_categories)的列表。

我想显示我的产品及其添加到的类别。 我正在尝试使用以下代码进行此操作:

Product::findOrFail($id)

但是此代码不起作用。该代码仅显示产品,而没有有关产品所属类别的信息。

该如何解决?
我的项目在Laravel 5.8中。

  1. 迁移
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';
        });

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_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');
        });         
  1. 模型
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 categories()
    {
        return $this->belongsToMany(Category::class, 'product_selected_categories', 'product_id', 'category_id');
    }
}

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 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;
} 

3 个答案:

答案 0 :(得分:1)

如果您 知道 ,您将需要产品的类别,则可以使用QueryBuilder的with()函数来eager load个相关模型:

$product = Product::with('categories')->findOrFail($id);

如果您需要紧急加载多个 关系,则可以将数组传递给with()

$product = Product::with(['categories', ...])->findOrFail($id);

但是,如果您 可能 不需要产品类别,则可以使用load()函数延迟加载类别:

$product = Product::findOrFail($id);

// ... some time later you end up needing the Categories...

$product->load('categories');

再一次,如果需要,您可以将数组传递给load()

$product->load(['categories', ...]);

答案 1 :(得分:0)

除了上面列出的以外,另一种选择是在$with模型上定义Product属性:

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;

  protected $with = ['categories'];
  ...
}

这是使用预加载的另一种方法。仅当您知道每次加载一个(或多个)产品时,您将 始终 需要每个加载的产品类别,才应使用该选项。 (请参阅“默认情况下急切加载” here

答案 2 :(得分:-2)

您可以通过以下方式加载关系:

Product::findOrFail($id)->with('categories');