siusiak1000
1 year ago

Return products with select subcategories

Posted 1 year ago by siusiak1000

I want to display products with the selected sub-category.

Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'projekt_pelka.magazine_subcategory_product' doesn't exist

I have:

Route::get('/magazyn/{kategoria}/{subkategoria}', '[email protected]_kat_sub');

SearchController:

    public function sklep_kat_sub($kategoria, $subkategoria)
    {
        $category = Product::with('subcategories')->where('name', $subkategoria)->firstOrFail();
        return $category;
    }

Migrations:

class CreateProductSubcategoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_subcategory', function (Blueprint $table) {
             $table->increments('id');
             $table->integer('product_id')->unsigned();
             $table->integer('subcategory_id')->unsigned();
        });

        Schema::table('product_subcategory', function (Blueprint $table) {
             $table->foreign('product_id')->references('id')->on('products');
        });

        Schema::table('product_subcategory', function (Blueprint $table) {
             $table->foreign('subcategory_id')->references('id')->on('magazine_subcategories');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('product_subcategory');
    }
}
class MagazineCategoriesSubcategories extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('magazine_category_subcategory', function (Blueprint $table) {
             $table->increments('id');
             $table->integer('category_id')->unsigned();
             $table->integer('subcategory_id')->unsigned();
        });

        Schema::table('magazine_category_subcategory', function (Blueprint $table) {
             $table->foreign('category_id')->references('id')->on('magazine_categories');
        });

        Schema::table('magazine_category_subcategory', function (Blueprint $table) {
             $table->foreign('subcategory_id')->references('id')->on('magazine_subcategories');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
class MagazineSubcategories extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('magazine_subcategories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

class MagazineCategories extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('magazine_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Models: Magazine_subcategory:

class Magazine_subcategory extends Model
{
    protected $fillable = [ 'name' ];
    protected $table = 'magazine_subcategories';

    public function categories()
    {
        return $this->belongsToMany('App\Magazine_category', 'magazine_category_subcategory',  'subcategory_id', 'category_id');
    }

    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

Product:

class Product extends Model
{


    protected $fillable = [
        'nazwa',
        'cena',
        'kopis',
        'dopis',
        'n_img',
        'subkategoria',
    ];

    public function subcategories()
    {
        return $this->belongsToMany('App\Magazine_subcategory', 'product_subcategory', 'product_id', 'subcategory_id');
    }
}

Connect_magazine_subcategory_category

class Connect_magazine_subcategory_category extends Model
{
    protected $table = 'magazine_category_subcategory';
    protected $fillable = [ 'kategoria', 'subkategoria' ];
}

Please sign in or create an account to participate in this conversation.