0
如何從數據庫中獲取模型,然後使用with語句將其轉換爲包含額外信息的數組。將模型轉換爲數組並返回爲json
public function edit($id) {
// convert product to array;
$product = Product::findOrFail($id)->with('supplier', 'category');
$data = [
'suppliers' => Supplier::all()->pluck('company', 'id'),
];
// cannot merge because $product is object and cannot turn into array
// the only way I know to convert to array is doing this
// $product->first()->toArray() but this gets the first item in the database
$product = array_merge($product, $data);
return response()->json($product, 200, ['Content-Length' => strlen(json_encode($product))]);
}