2016-08-01 2 views
1

У меня есть продукт модель, которая должна быть связана с ProductOptions и ProductOptionValues ​​ моделей.Laravel сфера запросов с вложенными связями belongsTo

В Продуктах :: 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 метод в продукты модели, которая вызывается из обработчика API JSON, но не могу понять, как гнездо & фильтра значения параметров и значений опций для представления массива JSON как тот, который был опубликован в самом начале вопроса.

ответ

1

Вы пытались с нетерпением загружать?

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