首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何返回同一资源LARAVEL中透视表的数据

如何返回同一资源LARAVEL中透视表的数据
EN

Stack Overflow用户
提问于 2019-07-24 05:03:00
回答 3查看 1.2K关注 0票数 2

关于我的LARAVEL应用程序接口,我有一个小问题。如何返回和拼接同一资源中透视表的数据?我有3个表,库存,产品和inventories_products。最后一个表有库存和价格数据(产品的数据,因为它们随库存而变化),我想列出产品并显示价格和库存(来自数据透视表)。

我上传了产品控制器、库存和产品型号以及产品资源。顺便说一下,我现在做的时候,价格和股票的返回值都是空的。

到目前为止,在我的ProductController中:

代码语言:javascript
代码运行次数:0
运行
复制
public function index()
{
   return ProductResource::collection(Product::with('inventories')->paginate(25));
}

在我的产品模型中:

代码语言:javascript
代码运行次数:0
运行
复制
class Product extends Model
{    
    public function inventories()
    {       
        return $this->belongsToMany('App\Inventory','inventory_product')->withPivot('price','stock')->withTimestamps();     
    }
}

在我的库存模型中:

代码语言:javascript
代码运行次数:0
运行
复制
class Inventory extends Model
{   
    public function products()
    {  
        return $this->belongsToMany('App\Product','inventory_product')->withPivot('price','stock')->withTimestamps();        
    }
}

在我的产品资源中:

代码语言:javascript
代码运行次数:0
运行
复制
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
    ];
}

我的迁移清单表:

代码语言:javascript
代码运行次数:0
运行
复制
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');
});

我的迁移产品表:

代码语言:javascript
代码运行次数:0
运行
复制
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迁移表:

代码语言:javascript
代码运行次数:0
运行
复制
$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');

有了这个,我得到了:

代码语言:javascript
代码运行次数:0
运行
复制
{
    "id": 1,
    //staff on product,
    "price": null,
    "stock": null
}

我应该得到的是:

代码语言:javascript
代码运行次数:0
运行
复制
{
    "id": 1,
    //staff on product,
    "price": 123,//data on the pivot table
    "stock": 123//data on the pivot table
}

编辑:实际上我应该得到这样的东西:

代码语言:javascript
代码运行次数:0
运行
复制
{
    "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
]

}

在产品会有多个库存的情况下,对吗?

提前感谢您:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-24 05:19:10

您的产品可能在多个库存中,您没有确定您从哪个库存中获取物品,您可以使用$this-> inventory put访问它,您不需要这样做,答案取决于您的逻辑,如果一个产品可能在多个库存中,您应该返回库存集合或计算库存总和或任何您需要查看的内容,如果一个库存中有一个产品,您应该将函数编辑为belongsTo,您的代码应该可以工作

票数 1
EN

Stack Overflow用户

发布于 2019-07-24 05:10:45

我认为问题出在index()函数试图返回一个Product Model集合,该集合将只包含该Model的参数。如果你只想要整个数组,你可以在这个集合上做一个连接:

https://laravel.com/docs/5.8/queries#joins

票数 1
EN

Stack Overflow用户

发布于 2019-07-24 05:30:28

您的关系是多对多的,如果您需要访问数据透视表,则此关系可以从相关行中获取一个产品和第一个相关库存或其他相关数据。例如,在您的资源中可以访问数据透视表

代码语言:javascript
代码运行次数:0
运行
复制
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
    ];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57172175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档