Laravel where with with with()

时间:2017-07-06 06:47:53

标签: php laravel laravel-5.4 laravel-query-builder

我想传递ids数组并获取category的所有ids以及category的所有横幅

我在ProductMainCategory.php模型

中定义了关系
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductMainCategory extends Model
{
    protected $hidden = ['created_at','updated_at'];

    public function banners()
    {
        return $this->hasMany('App\ProductCategoryBanner', 'category_id');
    }
}

ProductCategoryBanner.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductCategoryBanner extends Model
{
    protected $hidden = ['created_at','updated_at'];
}

在我的控制器中

   //get distinct main_categories by id
    $main_category_ids = Product::whereIn('id', $product_ids)
                                ->select('main_category_id')
                                ->distinct()
                                ->pluck('main_category_id')
                                ->toArray(); 

    //get main_category with banners (error query)
    $main_categories_with_images = ProductMainCategory::whereIn('id', $main_category_ids)
                                                      ->with(array('banners'=>function($query){
                                                            $query->select('id','image');
                                                       }))
                                                      ->get();

    //this query works, but it'll give all the categories but i want only passed id's categories
    $main_categories_with_images = ProductMainCategory::with('banners')
                                                  ->get();

我正在获取数据,但横幅是空的

    [
        {
            "id": 1,
            "name": "Grocery & Staples",
            "admin_id": 1,
            "banners": [] //getting empty array
        },
        {
            "id": 2,
            "name": "Fruits & Vegetables",
            "admin_id": 1,
            "banners": [] //getting empty array
        }
    ]

//actual result without $query->select('id','image'); is
 [
    {
        "id": 1,
        "name": "Grocery & Staples",
        "admin_id": 1,
        "banners": [
            {
                "id": 1,
                "category_id": "1",
                "image": "link 1",
                "admin_id": 2
            },
            {
                "id": 2,
                "category_id": "1",
                "image": "link 2",
                "admin_id": 1
            }
        ]
    },
    {
        "id": 2,
        "name": "Fruits & Vegetables",
        "admin_id": 1,
        "banners": [
            {
                "id": 3,
                "category_id": "2",
                "image": "link 3",
                "admin_id": 1
            },
            {
                "id": 4,
                "category_id": "2",
                "image": "link 4",
                "admin_id": 1
            }
        ]
    }
]

3 个答案:

答案 0 :(得分:1)

 $main_categories_with_images = ProductMainCategory::whereIn('id', $main_category_ids)
                                                  ->with(array('banners'=>function($query){
                                                        $query->select('id','image');
                                                   }))
                                                  ->get();

WareIn更改为WhereIn

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductCategoryBanner extends Model
  {
  protected $hidden = ['created_at','updated_at'];

  public function mainCategory() { return $this->belongsTo('ProductMainCategory')->select(['id','image'‌​]); }
 }

答案 1 :(得分:1)

改变这个:

 //get main_category with banners (error query)
    $main_categories_with_images = ProductMainCategory::whereIn('id', $main_category_ids)
                                                      ->with(array('banners'=>function($query){
                                                            $query->select('id','image');
                                                       }))
                                                      ->get();

答案 2 :(得分:0)

您在ProductMainCategory之后输了错误:wareIn(),它应该是whereIn()