我有一个多步骤注册表单,该表单会将具有相同提交的数据发送到三个不同的资源控制器(以存储功能),并建立模型。
表单控制器:
class Registration extends Controller
{
public function index() {
return view('admin.registration');
}
public function store(Request $r) {
Controller1::store($r->step1)
Controller2::store($r->step2)
Controller3::store($r->step3)
}
}
怎么可能是个好习惯?
答案 0 :(得分:1)
不需要调用3个不同的控制器,而是应该与3个不同的模型进行交互。根据您的代码,我假设您正在将表单提交到Registration@store
。代码如下
假设您有3步表格
提交表单后,将按store
方法。我还假设您有3张桌子和3 modal
,即User, Profile, CompanyProfile
。
如果是这种情况,我的方法就是这样
public function store(Request $request) {
$data['name'] = $request->get('name');
$data['email'] = $request->get('email');
....................................
....................................
$user = User:create($data) //This will create your user modal instance
//Now upload the 2nd steps data
$step2['user_id'] = $user->id;
$step2['locality'] = $request->get('locality');
.....................................
....................................
Profile::create($step2);
$step3['user_id'] = $user->id;
$step3['locality'] = $request->get('locality');
.....................................
....................................
CompanyProfile::create($step3);
return redirect('/home') //or whereever you want to redirect your user
}