How do I get a model from the database and then convert it to an Array including extra information using the with statement.
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))]);
}
via ONYX