Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有嵌套belongsTo关系的Laravel范围查询_Php_Json_Laravel_Model - Fatal编程技术网

Php 具有嵌套belongsTo关系的Laravel范围查询

Php 具有嵌套belongsTo关系的Laravel范围查询,php,json,laravel,model,Php,Json,Laravel,Model,我有一个产品模型,需要与产品选项和产品选项值模型连接 Products::getAll()应返回一个JSON,其中包含连接到产品的选项的嵌套视图,产品选项值连接到产品选项,如下所示: products: [ { id: 1, name: "product 1", ... ... options: [ { id: 1, na

我有一个产品模型,需要与产品选项产品选项值模型连接

Products::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
                    }
                ]
            }
        ]
    }
因此,我创建了两个不同的表(省略了产品表创建迁移)

产品选项表格

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();
});
产品选项值

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);
    }

}
产品期权价值型号:

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);
    }

}
产品型号:

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数据中还包含嵌套在产品“options”键中的选项值?我已经在将scopeWithCompleteData方法用于从JSON API处理程序调用的产品模型中,但是我不明白如何嵌套和过滤选项的值和选项值,以表示一个JSON数组,就像问题一开始所发布的那样。

您是否尝试过快速加载

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