Laravel registration login with ajax no page reload

Suppose you created a site using only Laravel and html css, js and jquery. Suppose you need a review from the user. If you want to review any you need to login. And if you are not a registered user, login and give a review. What to do in this case is shown in this post


1.  first composer create-project laravel/laravel example-app   

Create a Laravel application with the command.

Then install the required packages. Suppose you take a BK review, if any user wants to give a review, he has to register and login. In this case, if a user has been asked before, you can register and login and give a review! Then most users will not give reviews. Here, if you submit a review and if you have time to login, then I have written the review with difficulty as a user, then I do the rest of the work. Here's how to login without reloading the page

in Registions controlloer 

 

public function ajaxregister(Request $request)

    {

         $validator = Validator::make($request->all(), [

            'name' => 'required|min:3|max:80',

            'phone' => 'required|numeric|digits:11|unique:users',

            // 'email' => 'min:3|max:180|email|unique:users',

            'password' => 'required|min:6|max:30',

        ]);

        if ($validator->fails()) {

            return response()->json([

                'success' => false,

                'errors' => $validator->errors()->all()

            ]);

        } else {

 

            $list = new User();

            $list->name = $request->name;

            $list->ip_address = $request->ip();

            $list->phone = $request->phone;

            $list->username = Str::random(8, 15);

            $list->email = strtolower(trim($request->email));

            $list->password = Hash::make($request->password);

            $list->image = 'not-found.jpg';

            $list->location = $request->location;

            $list->otpno = mt_rand(1000, 9999);

            $list->save();

 

            if ($list->id) {

                $url = "user opt proviewer url";

                $number = $request->phone;

                $text = ("Dear " . $request->name . ', ' . "Please Verify  The Code " . $list->otpno . ". Thank You. (Team sohibd)");

                $data = array(

                    'username' => 'userusername',

                    'password' => 'QKR2SPFN',

                    'number' => "$number",

                    'message' => "$text"

                );

 

                $ch = curl_init(); // Initialize cURL

                curl_setopt($ch, CURLOPT_URL, $url);

                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                $smsresult = curl_exec($ch);

                $p = explode("|", $smsresult);

                $sendstatus = $p[0];

                return response()->json(['success' => true]);

            } else {


 

                return response()->json(['success' => false]);

            }

        }

    }

 

for Login

 

    public function modallogin(Request $request, FlasherInterface $flasher)

    {

        // return response($request->all());

        $userinfo = User::where('phone', '=', $request->phoneemail)->first();

        if ($userinfo) {

            $token = Auth::attempt(['phone' => $request->phoneemail, 'password' => $request->password, 'status' => '1']);

 

            if ($token) {

                $flasher->addSuccess('Login Successfully');

                return response()->json([

                    'success' => true,

                    'message' => 'Login Success'

                ], 201);

            } else {

 

                return response()->json([

                    'success' => false,

                ], 401);

            }

        } else {

 

            $token = Auth::attempt(['email' => $request->phoneemail, 'password' => $request->password, 'status' => '1']);

            if ($token) {

                $flasher->addSuccess('Login Successfully');

                return response()->json([

                    'success' => true,

                    'message' => 'Login Success'

                ], 201);

            } else {

 

                return response()->json([

                    'success' => false,

                ], 401);

            }

        }

    }

 

To see the code of blade file, go to codepen site. For this, see the link below.

  Laravel Bike Revew 

See More

 

                                                                

How does Laravel handle a request? What we usually see is that a route from web.php goes to the controller and then a response is received. But the whole journey of the request may not be clear to many. So let's take a look at the Laravel Lifecycle.

You Also Like That