siusiak1000's avatar

How can I get products with sub-category assigned?

How can I get products that have a sub-category assigned?

I have tables: products, magazine_subcategories and pivot table magazine_category_subcategory.

Before I created the sub-category I used this:

$query = Input::get('s');
Product::where('nazwa', 'like', '%' . $query . '%');

I have a $subcategory variable from the paramete.

Route::get('/magazine/{category}/{subcategory}', 'SearchController@sklep_kat_sub');

Model Magazine_subcategory

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

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

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

Model 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');
    }
}
0 likes
1 reply
siusiak1000's avatar

When I simplified:

        $category = Magazine_subcategory::with('products')->where('name',$subkategoria)->firstOrFail();
        return $category;

Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'projekt_pelka.magazine_subcategory_product' doesn't exist (SQL: select products.*, magazine_subcategory_product.magazine_subcategory_id as pivot_magazine_subcategory_id, magazine_subcategory_product.product_id as pivot_product_id from products inner join magazine_subcategory_product on products.id = magazine_subcategory_product.product_id where magazine_subcategory_product.magazine_subcategory_id in (1))

What is this caused?

Please or to participate in this conversation.