如何在雄辩的Laravel内部发送带有功能的单独数组?

时间:2020-10-03 07:55:58

标签: laravel eloquent

我在LineSheet控制器中的代码中编写了以下内容,而items函数在LineSheet模型中,并且我有一个数组调用$ seasons。现在,我需要发送带有项目

的$ seasons数组

LineSheet控制器:

$linesheetItems = LineSheetItem::select('linesheet_id', 'items.season', 'items.amt_item')
    ->join('items', function ($join) {
        $join->on('items.amt_item', '=', 'linesheet_items.item_code');
        $join->on('items.company', '=', 'linesheet_items.company');
        $join->on('items.division', '=', 'linesheet_items.division');
    })
    ->where('linesheet_items.linesheet_id', '=', $id)
    ->groupBy('items.amt_item', 'items.season')
    ->get();

foreach ($linesheetItems as $linesheetItem) {
    $seasons[] = Season::where('code', $linesheetItem['season'])->first();
}

$linesheet = LineSheet::where('linesheet.id', '=', $id)
    ->select('linesheet.*')->with(['items', 'creator:id,username', 'updater:id,username'])
    ->first();

LineSheet模型:

public function items()
{
    return $this->hasMany('\App\Models\LineSheetItem', 'linesheet_id', 'id')
        ->join('items', function ($join) {
            $join->on('items.amt_item', '=', 'linesheet_items.item_code');
            $join->on('items.company', '=', 'linesheet_items.company');
            $join->on('items.division', '=', 'linesheet_items.division');
        })
        ->join('inventory_items', function ($join) {
            $join->on('inventory_items.item', '=', 'linesheet_items.item_code');
            $join->on('inventory_items.company', '=', 'linesheet_items.company');
            $join->on('inventory_items.division', '=', 'linesheet_items.division');
        })
        ->select('linesheet_items.linesheet_id', 'items.id', 'items.amt_item', 'items.image_name',
            'items.company', 'items.division', 'items.color_description', 'items.item_description',
            'items.season', 'items.wholesale_price', 'items.retail_price', 'items.color_code',
            'items.vendor_desc', 'items.vendor_code', 'inventory_items.on_hand', 'inventory_items.cost',
            'items.brand', 'items.category_code', 'items.category', 'items.fabric_code', 'items.fabric_desc')
        ->groupBy('linesheet_items.item_code', 'linesheet_items.company', 'linesheet_items.division');
}

具有项目的同时,我还需要附加$ seasons数组并获得如下响应

我希望最终输出如下:

"items": [
    {
        "linesheet_id": 44,
        "id": 61,
        "amt_item": "PS839730WT",
        "image_name": "image_name",
        "company": "01",
        "division": "PAP",
        "color_description": "colordes1",
        "item_description": "itemdes1",
        "season": "S1",
        "wholesale_price": "100",
        "retail_price": "100",
        "color_code": "colorcode1",
        "vendor_desc": "vendor_desc",
        "vendor_code": "vendor_code",
        "on_hand": "40",
        "cost": "3.70",
        "brand": "brand",
        "category_code": "category",
        "category": "category1",
        "fabric_code": "code1",
        "fabric_desc": "fabric_desc",
        seasons: [
            //array elements
        ]
    }
]

如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用belongsTo关系,然后急于加载它们。 LineSheetItem似乎属于会话。

因此您可以进行LineSheetItem::with('season')->get()

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse

https://laravel.com/docs/8.x/eloquent-relationships#eager-loading