J03Bukowski
J03Bukowski

Reputation: 132

Laravel - One form for different controllers

I have a multi step registration form which will send data with the same submission to three different resource controllers (to store function), and so models.

The form controller:

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)
    }
}

How could be a good practice?

Upvotes: 0

Views: 246

Answers (1)

user7747472
user7747472

Reputation: 1952

There is no need for calling 3 different controller, rather you should interact with 3 different model. From your code, I am assuming you are submitting your form to Registration@store. Here is how the code

Let's assume you have 3 step form

  1. Step 1: get User email, mobile password
  2. Step2: User addresses and country
  3. Step 3: User company profile

Once the form is submitted it hit the store method. I am also assuming you have 3 table and 3 modal i.e User, Profile, CompanyProfile.

If that's the case my method would be like this

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
}

Upvotes: 1

Related Questions