Это Laravel ProductController и Products имеет отношение многие ко многим с тегами.
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::with('tags')->latest()->get();
return response()->json($products);
}
В ответе json, если я сопоставляю продукты, продукт. tag возвращает массив объектов.
[{"name": "shirt"}, {"name": "red"}]
Есть ли способ получить только свойство name в с ('tags') на контроллере, например:
["shirt", "red"]
Также я пробовал что-то вроде этого:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::with(['tags' => function ($query) {
$result = $query->select(['name']);
return $result->get();
}])->latest()->get();
return response()->json($products);
}
Возможно ли фильтровать данные внутри функции тегов?