关于我的LARAVEL
应用程序接口,我有一个小问题。如何返回和拼接同一资源中透视表的数据?我有3个表,库存,产品和inventories_products。最后一个表有库存和价格数据(产品的数据,因为它们随库存而变化),我想列出产品并显示价格和库存(来自数据透视表)。
我上传了产品控制器、库存和产品型号以及产品资源。顺便说一下,我现在做的时候,价格和股票的返回值都是空的。
到目前为止,在我的ProductController中:
public function index()
{
return ProductResource::collection(Product::with('inventories')->paginate(25));
}
在我的产品模型中:
class Product extends Model
{
public function inventories()
{
return $this->belongsToMany('App\Inventory','inventory_product')->withPivot('price','stock')->withTimestamps();
}
}
在我的库存模型中:
class Inventory extends Model
{
public function products()
{
return $this->belongsToMany('App\Product','inventory_product')->withPivot('price','stock')->withTimestamps();
}
}
在我的产品资源中:
public function toArray($request)
{
return [
'id'=>$this->id,
'name'=>$this->name,
'description'=>$this->description,
'short_description'=>$this->short_description,
'category'=>$this->category,//category_id
'url'=>$this->url,
'image'=>$this->image,
'relevant'=>$this->relevant,
'month'=>$this->month,
'price'=>$this->price,
'stock'=>$this->stock
];
}
我的迁移清单表:
Schema::create('inventories', function (Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->unsignedInteger('city_id');
$table->timestamps();
$table-> foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
});
我的迁移产品表:
Schema::create('products', function (Blueprint $table)
{
$table->increments('id');
$table ->string('name');
//$table ->integer('stock');
$table ->string('description');
$table ->string('short_description');
$table ->unsignedInteger('category');//category_id
//$table ->integer('price');
$table ->string('url');
$table ->string('image');
$table ->boolean('relevant');
$table ->boolean('month');
$table->timestamps();
$table-> foreign('category')->references('id')->on('categories')->onDelete('cascade');
});
和我的inventory_product迁移表:
$table->increments('id');
$table->integer('inventory_id')->unsigned();
$table->integer('product_id')->unsigned();
$table ->integer('price');
$table ->integer('stock');
$table->timestamps();
$table-> foreign('inventory_id')->references('id')->on('inventories')->onDelete('cascade');
$table-> foreign('product_id')->references('id')->on('products')->onDelete('cascade');
有了这个,我得到了:
{
"id": 1,
//staff on product,
"price": null,
"stock": null
}
我应该得到的是:
{
"id": 1,
//staff on product,
"price": 123,//data on the pivot table
"stock": 123//data on the pivot table
}
编辑:实际上我应该得到这样的东西:
{
"id": 1,
//staff on product,
[
"inventory_id": 1,//data on the pivot table
"price": 123,//data on the pivot table
"stock": 123//data on the pivot table
]
[
"inventory_id": 2,//data on the pivot table
"price": 333,//data on the pivot table
"stock": 333//data on the pivot table
]
}
在产品会有多个库存的情况下,对吗?
提前感谢您:)
发布于 2019-07-23 21:19:10
您的产品可能在多个库存中,您没有确定您从哪个库存中获取物品,您可以使用$this-> inventory put访问它,您不需要这样做,答案取决于您的逻辑,如果一个产品可能在多个库存中,您应该返回库存集合或计算库存总和或任何您需要查看的内容,如果一个库存中有一个产品,您应该将函数编辑为belongsTo,您的代码应该可以工作
发布于 2019-07-23 21:10:45
我认为问题出在index()函数试图返回一个Product Model集合,该集合将只包含该Model的参数。如果你只想要整个数组,你可以在这个集合上做一个连接:
发布于 2019-07-23 21:30:28
您的关系是多对多的,如果您需要访问数据透视表,则此关系可以从相关行中获取一个产品和第一个相关库存或其他相关数据。例如,在您的资源中可以访问数据透视表
public function toArray($request)
{
return [
'id'=>$this->id,
'name'=>$this->name,
'description'=>$this->description,
'short_description'=>$this->short_description,
'category'=>$this->category,//category_id
'url'=>$this->url,
'image'=>$this->image,
'relevant'=>$this->relevant,
'month'=>$this->month,
'price'=>$this->inventories->first->price,
'stock'=>$this->inventories->first->stock
];
}
https://stackoverflow.com/questions/57172175
复制