i have product model needs connected productoptions , productoptionvalues models.
the products::getall() should returned json contains nested view of options connected product, product option values connected product option, in way:
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 } ] } ] }
so, have created 2 different tables ( product table create migration omitted )
product options table
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(); });
product option values table
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(); });
product option model:
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); } ... ... }
product option value model:
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); } ... ... }
product model:
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); } }
question is, how can product object contains, in json data, option values nested "options" key of product? i'm using scopewithcompletedata method products model called json api handler, can't understand how nest & filter values of options , option values represent json array 1 posted @ beginning of question.
did try eager loading?
product::with('product_option_values')->get()->tojson()
Comments
Post a Comment