0
votes

I have one form in frontend where I have there is some city details , rooms details and user registration in one form like I have city name , room name , address etc email addresss and password in same form and I have done 2 logics in one controller for creating cities and registering user It is saving the both data in correct table in the database but I want that first user should register and if user is vcerified only the room details should be saved in database

I am in confusion wheather to apply if again or what

public function checkLogin(Request $request)
{
  $user = User::create([
    'name'=>$request->name,
    'email'=>$request->email,
    'password'=>$request->password,
    'role_id' => config('quickadmin.default_role_id'),
  ]);
  if ($user) {
      if (Auth::check()) {
        $city = TotalCity::create([
            'name'=>$request->name,
            'created_by'=>$request->created_by_id,
        ]);
      }
       return redirect()->to('/admin/home');
  }
}
1
With User::create() you are creating a new user every time you call checkLogin(), right? Is this your expected behaviour? Maybe you should check if this user exists first (what if you're trying to save the same email twice)senty
Instead of Auth::check(), how about if ($user->email_verified_at)? However, your logic is a bit off for a checkLogin() methodsenty
@senty Yes if email is same it says duplicate email my problem is that it is not showing the data in the backend of that particular user who created thatBipin Regmi
@senty Tried to answer It is not saving those city data in database and it is neighter redirecting the page to /admin/home after I hit submitBipin Regmi
I fixed the redirect code in the answer, however if Auth::check() is failing, that means you don't have authenticated user. Do you want the created user to be logged in at the time of creation? I believe your logic is wrong. I believe you need to use Auth\RegisterController@registered to apply this logic entirelysenty

1 Answers

0
votes

Let me show you how I'd probably write this logic:

public function checkLogin(Request $request)
{
  $user = User::firstOrCreate([
    'email'=> $request->email,    
  ], 
  [
    'name'=> $request->name,
    'password'=> bcrypt($request->password),
    'role_id' => config('quickadmin.default_role_id'),
  ]);

  if (Auth::check()) {  
  // it's not clear if you utilize `email_verified_at`, if so 
  // if (Auth::check() && Auth::user()->email_verified_at) {

    $city = TotalCity::create([
        'name'=>$request->name,
        'created_by'=> Auth::user()->id, // or $user->id depending on your preference
    ]);
  }
  return redirect('/admin/home');
}

The firstOrCreate() checks if an entry with that email exists, it gets it, otherwise creates it.


Furthermore, if I want to check for Authentication, I'd use 'auth' middleware in my route.

Route::get('example', 'ExampleController@checkLogin')->middleware('auth');

That removes the need of entire check:

if (Auth::check()) { ... }