2016-08-01 99 views
1

我有一個產品模式,需要與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數組就像問題開始時發佈的那樣。

回答

1

您是否嘗試過急於加載?

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