简体   繁体   English

Laravel model 未在资源 api 中获取关系

[英]Laravel model not get relations in resource api

I have 2 tables that are related to each other by 3rd table (where only stores the ids) now seems i can't get my data out of third table in API resource file我有 2 个表,它们通过第三个表(仅存储 id)相互关联,现在似乎我无法从 API 资源文件中的第三个表中获取我的数据

Logic逻辑

  1. Product model (has many barcode) Product model(有很多条码)
  2. Barcode model (belongs to product and belongs to outlet) Barcode model(属于产品属于奥特莱斯)
  3. Outlet model (has many barcodes) Outlet model(有很多条码)
  4. outlet_products table (stores barcode_id and outlet_id ) outlet_products表(存储barcode_idoutlet_id

Code代码

Barcode model

class Barcode extends Model
{
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function outlet()
    {
        return $this->belongsTo(Outlet::class, 'outlet_products', 'barcode_id', 'outlet_id');
    }
}

Outlet model

class Outlet extends Model
{
    public function barcodes()
    {
        return $this->hasMany(Barcode::class, 'outlet_products', 'outlet_id', 'barcode_id');
    }
}

BarcodeResource

class BarcodeResource extends JsonResource
{
    public function toArray($request)
    {
        $arrayData = [
            'id' => $this->id,
            'sku' => $this->sku,
            'serial_number' =>  $this->serial_number ? (Int) $this->serial_number : null,
            'price' => (Int) $this->price,
            'discount' => $this->discount ? (Int) $this->discount : null,
            'product' => new ProductsResource($this->whenLoaded('product')),
            'outlet' => new OutletsResource($this->whenLoaded('outlet')),
        ];
        return $arrayData;
    }
}

Now I am trying to get my product barcodes and name of each barcode outlet.现在我正在尝试获取我的产品条形码和每个条形码出口的名称。

Controller

$products = ProductsResource::collection(Product::orderBy('id', 'desc')->with(['barcodes', 'barcodes.outlet'])->get());

and the result is:结果是:

一

Any idea why i can't get my barcodes outlet?知道为什么我的条形码出不来吗?

Based on your question and comment above:根据您上面的问题和评论:

  • A Product has many Barcode and a Barcode belongs to a Product .一个Product有很多Barcode ,一个Barcode属于一个Product They have a One To Many relationship.他们有一对多的关系。

  • An Outlet has many barcode and a Barcode has many Outlet .一个Outlet有很多barcode ,一个Barcode有很多Outlet They have a Many To Many relationship.他们有一个多对多的关系。


Here's how to set the One To Many relationship.这是设置一对多关系的方法。

In your Product class:在您的Product class 中:

class Product extends Model
{
    public function barcodes()
    {
        return $this->hasMany('App\Barcode');
    }
}

In your Barcode class, set the inverse relationship:在您的Barcode class 中,设置反比关系:

class Barcode extends Model
{
    public function product()
    {
        return $this->belongsTo('App\Product');
    }
}

Here's how to set the Many To Many relationship.这是设置多对多关系的方法。

In your Outlet class:在您的Outlet class 中:

class Outlet extends Model
{
    public function barcodes()
    {
        return $this->belongsToMany('App\Barcode');
    }
}

In your Barcode class:在您的Barcode class 中:

class Barcode extends Model
{
    public function outlets()
    {
        return $this->belongsToMany('App\Outlet');
    }
}

With a Many to Many relationship, you also need to create an intermediate table.对于多对多关系,您还需要创建一个中间表。

In this case, your pivot table will be barcode_outlet .在这种情况下,您的 pivot 表将为barcode_outlet

Important: Per Laravel convention, the intermediate table name must be in singular form and alphabetical order of the related model names (barcode_outlet in your case).重要提示:根据 Laravel 约定,中间表名称必须是相关 model 名称(在您的情况下为 barcode_outlet)的单数形式和字母顺序。

The intermediate table must have barcode_id and outlet_id columns.中间表必须有barcode_idoutlet_id列。 I will let you modify your migrations accordingly.我会让你相应地修改你的迁移。


After setting the relationships, you will be able to use Eloquent to retrieve your models.设置关系后,您将能够使用 Eloquent 检索模型。

Example:例子:

$barcode->outlets;
// returns a Collection of outlets

$barecode->product;
// returns a Product

$product->barcodes;
// returns a Collection of barcodes

$outlet->barcodes;
// returns a Collection of barcodes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM