Saturday, February 17, 2018

Getting latest products base on their category in laravel - development

Getting latest products base on their category in laravel

I try to get my latest products of each category in separate panel in my index page,

for this matter there are 3 tables involved products admin_designs and categories

Logic

In my admin_designs table i will select categories and base on my selected category there will add new panel to my index page including last 10 products in that category.

So far I can get my categories products in index page in separate
panels BUT not as I wish.

Problems

  1. I’m not able to get categories title
  2. I only get 1 product as result however I limited results for 10
  3. I want to get title value from admin_designs table instead of category title if is provided only but i can’t find it in my query.

codes

controller

    public function welcome() {
        //rest of the codes....

        //dynamic categories
        $category = DB::table('admin_designs')
                  ->join('categories', 'categories.id', '=', 'admin_designs.category_id')
                  ->join('products', function ($join) {
                    $join->on('admin_designs.category_id', '=', 'products.category_id');
                  })
                  ->groupby('categories.id')
                  ->take(10)
                  ->get();
        return view('welcome', compact('category'));
}

my blade code

@foreach($category as $ki)
  // title of panel
  cat id:   //return category id from products table (the best i could get instead of category title!)

  //panel data
   // product title
@endforeach

NOTE: as you see in my controller i have ->take(10) but my result only get 1 (I have enough products in each category so is not about that i don’t have data)

Screenshot

this is actual results

screenshot1

UPDATE

categories schema I need title from this table.

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->unique();
            $table->string('image');
            $table->string('imageAlt')->nullable();
            $table->string('slug');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->integer('status_id')->unsigned();
            $table->timestamps();
        });
        Schema::table('categories', function (Blueprint $table) {
            $table->foreign('status_id')->references('id')->on('statuses');
        });

Admin_designs schema I need title from this table

Schema::create('admin_designs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned();
            $table->integer('status_id')->unsigned();
            $table->string('title')->nullable();
            $table->timestamps();
        });
        Schema::table('admin_designs', function($table) {
           $table->foreign('category_id')->references('id')->on('categories');
           $table->foreign('status_id')->references('id')->on('statuses');
        });

products schema I need everything from this table

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->unique();
            $table->string('slug');
            $table->string('imageOne');
            $table->string('imageTwo')->nullable();
            $table->text('short_description');
            $table->longText('description');
            $table->string('price');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->string('arrivalDays');
            $table->string('height')->nullable();
            $table->string('weight')->nullable();
            $table->string('lenght')->nullable();
            $table->string('width')->nullable();
            $table->string('sku');
            $table->string('stock');
            $table->integer('status_id')->unsigned();
            $table->integer('brand_id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->integer('subcategory_id')->unsigned();
            $table->timestamps();
        });
        Schema::table('products', function (Blueprint $table) {
            $table->foreign('status_id')->references('id')->on('statuses');
            $table->foreign('brand_id')->references('id')->on('brands');
            $table->foreign('category_id')->references('id')->on('categories');
            $table->foreign('subcategory_id')->references('id')->on('subcategories');
        });

Relations

category.php

public function products(){
     return $this->hasMany(Product::class);
  }
  public function admin_designs(){
     return $this->hasMany(AdminDesign::class);
  }

admindesign.php

public function category(){
        return $this->belongsTo(Category::class, 'category_id');
}

product.php

public function category(){
     return $this->belongsTo(Category::class);
  }



from Laravel Questions and Answers https://laravelquestions.com/php/getting-latest-products-base-on-their-category-in-laravel/
via Lzo Media

No comments:

Post a Comment