Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有类别/子类别名称的产品列表_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 具有类别/子类别名称的产品列表

Php 具有类别/子类别名称的产品列表,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8 我有这个模型: class ProductCategory extends Model { use scopeActiveTrait; protected $guarded = ['id']; protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'level', 'parent_id', 'number'];

我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8

我有这个模型:

class ProductCategory extends Model
{
    use scopeActiveTrait;

    protected $guarded = ['id'];
    protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'level', 'parent_id', 'number'];
    public $timestamps = false;
    //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');
    }
}

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;
}
和模式:

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->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')->onDelete('cascade');;
        });
一个产品可分配到1个类别和8个子类别(产品所选类别)

我需要显示与类别和子类别的产品清单

我有以下代码来显示产品列表:

Product::with(['producer', 'selected_categories'])->orderBy($sortColumn, $sortMethod)->paginate(25);

@foreach($products as $product)
{{ $product->name }} - ___product category____  - ___product subcategory1____  - ___product subcategory2____  - ___product subcategory3____ ....  - ___product subcategory8____ 
如何显示产品分配到的类别和子类别


请帮帮我。

我认为如果你们在产品和类别之间有很多联系会更好

考虑到这一点,这会容易得多

$product = App\Product::find(1);
foreach ($product->categories as $category) {
     //
}
你们有很多种类和产品。 每种产品都会有很多种类,很多种类都会有很多产品。您将拥有多行产品和一个类别,而不是一行多类别。如果要添加子类别类型,可以在透视表中添加一个额外的列。

它不起作用:(我有错误:SQLSTATE[42S02]:找不到基表或视图:1146表“Base.product\u product\u category”不存在(SQL:选择
product\u categories
*,
product\u category
product\u id
作为
pivot\u product\u id
product\u category
作为
product\u categories\u id
product\u categories
内部连接
product\u product\u categories
de>产品类别
id
=
产品类别
产品类别
其中
产品类别
产品id
=2)
$product = App\Product::find(1);
foreach ($product->categories as $category) {
     //
}