Laravel 拉维尔雄辩关系

Laravel 拉维尔雄辩关系,laravel,eloquent,Laravel,Eloquent,我有两张桌子 产品:id、代码、详细信息,创建于 价格修订:标识、产品标识、修订价格、用户、备注、创建日期、更新日期 我希望生成以下json: { "accessory_price":{[ "date":"2015-03-01", "products": {[ "product_id":1, "product_name":"test accessory 1", "revised_price":50.00, "user":"samyak", "remarks":"n/a" ], [ "produ

我有两张桌子

产品:id、代码、详细信息,创建于

价格修订:标识、产品标识、修订价格、用户、备注、创建日期、更新日期

我希望生成以下json:

 {
"accessory_price":{[
"date":"2015-03-01",
"products":
{[ "product_id":1, "product_name":"test accessory 1", "revised_price":50.00, "user":"samyak", "remarks":"n/a" ], [ "product_id":2, "product_name":"test accessory 2", "revised_price":60.00, "user":"samyak", "remarks":"n/a" ], [ "product_id":3, "product_name":"test accessory 3", "revised_price":70.00, "user":"samyak", "remarks":"n/a" ]}
],
[
"updated_at":"2015-02-01",
"products":
{[ "product_id":1, "product_name":"test accessory 1", "revised_price":40.00,    "user":"sam", "remarks":"n/a" ], [ "product_id":2, "product_name":"test accessory 2", "revised_price":50.00, "user":"sam", "remarks":"n/a" ], [ "product_id":3, "product_name":"test accessory 3", "revised_price":60.00, "user":"sam", "remarks":"n/a" ]}
]
]}
我制作了这样的模型:

产品型号

public function productpricerevision(){
 return $this->hasMany('Modules\Quotes\Entities\ProductPriceRevision','product_id');

}
存储库代码

 public function getProductPrice(){
$productprice=ProductModel::with('productpricerevision')->get();            
 $data=array('accessory_price'=>$productprice);
  return $data;

 }
但是有了这个密码我就明白了

{
  "product_price": [
{
  "id": 1,

  "code": "G4007",     
  "detail": "Flyscreen Gasket -4007",
  "created_at": "2014-07-11 13:53:02",
   "productprice": [
    {
      "id": 1,
      "product_id": 1,
      "revised_price": 78,
      "user": "sam",
      "remark": "test",
      "created_at": "2016-03-30 00:00:00",
      "updated_at": "2016-03-30 00:00:00"
    }
  ]
},
{
  "id": 2,
  "code": "CZBR-100S",
  "detail": "Single Bearing Roller BR-100-S",
   "created_date": "2014-07-11 13:57:15",
   "productprice": [
    {
      "id": 4,
      "product_id": 2,
      "revised_price": null,
      "user": sam,
      "remark": null,
      "created_at": "2016-03-30 00:00:00",
      "updated_at": "2016-03-30 00:00:00"
    }
  ]
},
产品迁移

class CreateProductsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('products', function(Blueprint $table)
    {
        $table->integer('id');
        $table->string('code', 25);
        $table->text('detail', 65535)->nullable();
        $table->timestamp('created_date')->default(DB::raw('CURRENT_TIMESTAMP'));
                });
}


/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('products');
}

}
 public function up()
{
    Schema::create('product_price_revision', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('product_id');
        $table->double('revised_price')->nullable();
         $table->string('user')->nullable();
         $table->text('remark')->nullable();
          $table->foreign('product_id')->references('id')->on('products')->onDelete('CASCADE')->onUpdate('CASCADE');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('product_price_revision');
}
价格修订

class CreateProductsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('products', function(Blueprint $table)
    {
        $table->integer('id');
        $table->string('code', 25);
        $table->text('detail', 65535)->nullable();
        $table->timestamp('created_date')->default(DB::raw('CURRENT_TIMESTAMP'));
                });
}


/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('products');
}

}
 public function up()
{
    Schema::create('product_price_revision', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('product_id');
        $table->double('revised_price')->nullable();
         $table->string('user')->nullable();
         $table->text('remark')->nullable();
          $table->foreign('product_id')->references('id')->on('products')->onDelete('CASCADE')->onUpdate('CASCADE');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('product_price_revision');
}

我也尝试过其他几种方法,但最后还是迷路了。非常感谢您的帮助。

您可以发布迁移以创建表吗?@Andersc我已经添加了迁移。谢谢您可以发布迁移以创建表吗?@Andersc我已经添加了迁移。谢谢