Eager-load relations the admin read API returns
Edit forms are populated from a single request, so posts need their tags and projects their category attached; otherwise the tag multi-select and category picker come back empty.
This commit is contained in:
1 parent
3f386c7be8
commit
5ae1d1a632
1 file changed
+12
-1
@@ -28,6 +28,17 @@ class AdminResourceController extends Controller
|
||||
'about-me' => AboutMe::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Relations eager loaded per resource, so an editing form is populated
|
||||
* from a single request instead of a fan-out of lookups.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
private const RELATIONS = [
|
||||
'posts' => ['tags'],
|
||||
'projects' => ['category'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Raw rows for content management.
|
||||
*
|
||||
@@ -44,7 +55,7 @@ class AdminResourceController extends Controller
|
||||
return response()->json(['message' => 'Unknown admin resource.'], 404);
|
||||
}
|
||||
|
||||
$rows = $model::query()->orderByDesc('id')->get();
|
||||
$rows = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id')->get();
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user