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

标签 php laravel eloquent

我对我的 LARAVEL API 有一个小问题。如何返回并连接同一资源中的数据透视表的数据?我有 3 个表,库存、产品和 inventory_products。最后一个表包含库存和价格数据(产品的,因为它们根据库存而变化),我想列出产品并显示价格和库存(来自数据透视表)

我上传产品 Controller 、库存和产品模型以及产品资源。顺便说一句,正如我现在所做的那样,价格和股票返回为空。

到目前为止,在我的 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
    ];
}

我的迁移 list 表:

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
]

}

如果该产品出现在多个库存中,对吧?

提前谢谢你:)

最佳答案

您的产品可能位于多个库存中,您无法确定您从哪个库存中获取商品,您可以使用 $this->inventories 访问它 说你不需要那个, 答案取决于您的逻辑,如果一种产品的库存量可能超过您的库存,您应该返回库存集合或对库存求和或您需要查看的任何内容, 如果 1 个库存中存在 1 个产品,您应该将函数编辑为“belongsTo”,并且您的代码应该可以工作

关于php - 如何在同一资源LARAVEL中返回数据透视表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57172175/

相关文章:

php - 仅在 mysql 数据库中插入一次。

php - 检测两个数字范围是否冲突

php - 自动注入(inject)的 Laravel 模型没有属性

php - 如何将json字段中的数组数据显示到 Blade View ?

php - 如何在 Laravel 中级联软删除?

php - PHP自定义错误处理程序: How to determine if exception was generated within try{}catch{} block?

php - 使用 Blowfish 和 ECB 将 mcrypt 迁移到 OpenSSL

php - 如何使个人资料页面可供用户编辑,我想在编辑后保存这些数据(vue.js + laravel)

php - Codeigniter/Eloquent 选择但不插入

php - Laravel eloquent 如何获取不为NULL 的最小值?