Скажем, у меня есть две модели SomeModel и SomeRelatedModel, где some_related_model.some_model_id — это FK для SomeModel.
Стандартный метод SomeModelController для обработки POST создания /api/someModel может выглядеть так:
Код: Выделить всё
public function store(Request $request)
{
$user = Auth::guard('api')->user();
$data = $request->get('data');
$data['user_id'] = $user->id;
$someModel = SomeModel::create($data);
// has this request been made with the data for the
// related model? If so create this too.
if($data['relatedModel']){
SomeRelatedModel::create(array_merge(
['some_model_id' => $someModel->id]
$data['relatedModel']
));
}
// has this request been made expecting to get related
// models back in the response? If so load these
if($request->has('with')){
$someModel->load($request->get('with'));
}
return (new PostResource($post))
->toResponse($request)
->setStatusCode(201);
}
Существует ли более универсальный (или аккуратный) шаблон с использованием готовых классов для получения аналогичного эффекта?
Подробнее здесь: https://stackoverflow.com/questions/732 ... ource-crud
Мобильная версия