简体   繁体   English

Laravel-几个相关的模型保存

[英]Laravel - Several related models saving

Laravel 5.4.* Laravel 5.4。*

I have the following controllers and functions: 我具有以下控制器和功能:

AuthorController AuthorController

class AuthorController extends Controller {
    public function store(StoreAuthor $request) {
        Author::create($request->all());
        return response()->json(["message" => "done"]);
    }
}

BookController BookController的

class BookController extends Controller {
    public function store(StoreBook $request) {
        Book::create($request->all());
        return response()->json(["message" => "done"]);
    }
}

CategoryController CategoryController

class CategoryController extends Controller {
    public function store(StoreCategory $request) {
        Category::create($request->all());
        return response()->json(["message" => "done"]);
    }
}

And more like these. 以及更多类似的东西。

I handle validation and security through the FormRequest, and this is clean and elegant. 我通过FormRequest处理验证和安全性,这很简洁。

What I'm trying to achieve has been asked before, but I haven't really found a solution that worked for me. 以前曾问过我要达到的目标,但是我还没有真正找到适合我的解决方案。

I want to send one form and save many models which are related. 我想发送一份表格并保存许多相关的模型。 I imagine i'd send a json object which contains data for all the models like this: {author:author, book:book, category:category, ....} 我想我会发送一个json对象,其中包含所有像这样的模型的数据: {author:author, book:book, category:category, ....}

Should I create a different controller which handles the logic of multiple models? 我应该创建一个处理多种模型逻辑的控制器吗? If so how do I avoid repeating the logic within the original controllers? 如果是这样,如何避免在原始控制器中重复逻辑?

Should i create a Trait for every controller that can be saved for other controllers (ie: AuthorTrait )? 我是否应该为可以保存到其他控制器的每个控制器(即AuthorTrait )创建一个特性? If so how can i keep the formRequest validation and policies logic? 如果是这样,我如何保持formRequest验证和策略逻辑?

I have seen the laravel methods associate and attach , but they seem to work only if the record has been already created. 我已经看到laravel方法associateattach ,但是它们似乎仅在记录已经创建时才起作用。

The first thing that comes to my mind is chaining different controllers methods, but that seems really unclean to me. 我想到的第一件事是链接不同的控制器方法,但这对我来说似乎很不干净。

Thank You. 谢谢。

It sounds to me like you want to create a service that handles these. 在我看来,您想要创建一个处理这些的服务。 I would say in general your feeling that chaining different controller methods is definitely not the way to handle this. 我通常会说您的感觉是,链接不同的控制器方法绝对不是解决此问题的方法。 You basically need a service that examines the request data and saves them appropriately. 基本上,您需要一种服务来检查请求数据并适当地保存它们。

In your controller: 在您的控制器中:

class AuthorController extends Controller {
    public function store(StoreAuthor $request, CustomService $service) {
        return $service->execute($request);
    }
}

This service would handle the logic of parsing and validating the data. 该服务将处理解析和验证数据的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM