嵌套属于关联关系Laravel范围查询

问题描述:

我有一个产品模式,需要与ProductOptionsProductOptionValues模型连接。嵌套属于关联关系Laravel范围查询

产品:: GETALL()应包含嵌套认为连接到产品的选项一个JSON返回,与连接到产品选择产品选项的值,以这样的方式

products: [ 
    { 
     id: 1, 
     name: "product 1", 
     ... 
     ... 
     options: [ 
      { 
       id: 1, 
       name: "option 1", 
       is_visible: 1, 
       description: "desc", 
       values: [ 
        { 
         id: 1, 
         name: "option value 1", 
         sku: "test 1", 
         description: "desc 1", 
         unitary_price: 5.5 
        }, 
        { 
         id: 2, 
         name: "option value 2", 
         sku: "test 2", 
         description: "desc 2", 
         unitary_price: 5.5 
        } 
       ] 
      }, 
      ... 
      { 
       id: 20, 
       name: "option 20", 
       is_visible: 0, 
       description: "desc 2", 
       values: [ 
        { 
         id: 30, 
         name: "option value 30", 
         sku: "test 30", 
         description: "desc 30", 
         unitary_price: 35.5 
        }, 
        { 
         id: 40, 
         name: "option value 40", 
         sku: "test 40", 
         description: "desc 40", 
         unitary_price: 45.5 
        } 
       ] 
      } 
     ] 
    } 

因此,我已经创建2个不同的表(省略产品表创建迁移)

产品选项

Schema::create('product_options', function (Blueprint $table) { 
    $table->increments('id'); 

    $table->string('name'); 
    $table->string('slug'); 
    $table->text('description'); 
    $table->boolean('is_visible')->index()->default(0); 

    $table->integer('product_id')->unsigned()->index(); 

    $table->timestamps(); 
    $table->softDeletes(); 
}); 

产品选项值

Schema::create('product_option_values', function (Blueprint $table) { 
    $table->increments('id'); 

    $table->string('name'); 
    $table->string('sku'); 
    $table->text('description'); 
    $table->boolean('is_default_value')->index()->default(0); 

    $table->integer('product_option_id')->unsigned()->index(); 
    $table->integer('product_id')->unsigned()->index(); 

    $table->decimal('unitary_price', 10, 2); 

    $table->timestamps(); 
    $table->softDeletes(); 
}); 

产品选项型号:

class ProductOption extends Model { 
    use SoftDeletes; 
    use SluggableTrait; 

    protected $dates = ['deleted_at']; 
    protected $guarded = ['id', 'created_at', 'updated_at']; 
    protected $sluggable = [ 
     'build_from' => 'name', 
     'save_to' => 'slug', 
     'include_trashed' => true 
    ]; 

    public function product() { 
     return $this->belongsTo(Product::class); 
    } 

    public function productOptionValues() { 
     return $this->hasMany(ProductOptionValue::class); 
    } 

    ... 
    ... 
} 

产品期权价值型号:

class ProductOptionValue extends Model { 
    use SoftDeletes; 
    use SluggableTrait; 

    protected $dates = ['deleted_at']; 
    protected $guarded = ['id', 'created_at', 'updated_at']; 

    public function product() { 
     return $this->belongsTo(Product::class); 
    } 

    public function productOption() { 
     return $this->belongsTo(ProductOption::class); 
    } 

    ... 
    ... 
} 

产品型号:

class Product extends Model implements SluggableInterface { 
    use SoftDeletes; 
    use SluggableTrait; 

    protected $dates = ['deleted_at']; 
    protected $guarded = ['id', 'created_at', 'updated_at']; 
    protected $sluggable = [ 
     'build_from' => 'name', 
     'save_to' => 'slug', 
     'include_trashed' => true 
    ]; 

    ... 
    ... 

    public function productOptions() { 
     return $this->hasMany(ProductOption::class); 
    } 

    public function productOptionValues() { 
     return $this->hasMany(ProductOptionValue::class); 
    } 

} 

的问题是,如何才能得到一个包含在JSON数据产品对象,也是期权价值嵌套到产品的“选项”键?我已经使用了scopeWithCompleteData方法进入产品模型,从JSON API处理器调用,但无法理解的选择和选项值的值鸟巢如何&过滤器来表示JSON数组就像问题开始时发布的那样。

您是否尝试过急于加载?

Product::with('product_option_values')->get()->toJson()