在laravel中获取基于其类别的最新产品

时间:2018-02-17 07:11:45

标签: php laravel

我尝试在索引页面的单独面板中获取每个类别的最新产品,

此事涉及3个表products admin_designscategories

逻辑

在我的admin_designs表格中,我会选择类别,base on my selected category我会将new panel添加到我的索引页面,包括该类别中的last 10个产品。

  

到目前为止,我可以在索引页面中单独获取我的类别产品   面板但不是我想要的。

问题

  1. 我无法获得类别标题
  2. 我只收到1个产品,但我限制10
  3. 的结果
  4. 我想从title表中获取admin_designs值,而不是类别标题如果仅提供,但我无法在查询中找到它。
  5. 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'));
    }
    

    我的blade代码

    @foreach($category as $ki)
      // title of panel
      cat id: {{$ki->category_id}}  //return category id from products table (the best i could get instead of category title!)
    
      //panel data
      {{$ki->title}} // product title
    @endforeach
    

    注意:正如您在我的控制器中看到的那样->take(10)但我的结果只有1(我的每个类别都有足够的产品,所以不是我的#39; t有数据)

    截图

    this is actual results

    screenshot1

    更新

    categories schema我需要此表中的标题。

    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我需要此表中的标题

    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我需要此表中的所有内容

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

    关系

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

    更新2

    我决定删除我的admin_designs表,而是在我的categories表中添加了一列,并在我的类别模型中添加了本地范围。现在,当我将新列值设置为1时,类别产品将显示在主页中。

    这是查询:

    $categories = Category::with('products')->OfListed()->get();
    

    ISSUE:

    我需要此查询才能获得我的类别中的最后10个产品,而不是更多。如何将该条件添加到其中?

0 个答案:

没有答案