Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

update a column to null in Laravel

How to update a column to null in Laravel?

Do you want to update a column to null in Laravel?

How to update a particular column values in laravel
How to Update Column to Null in laravel

This step-by-step tutorial helps you How to update particular column values in Laravel with the help of Eloquent in the Laravel application.

Need to Update null in a single column in Laravel

In the multiple application, we use user authentication to access the application platform or a user dashboard, so we verify the user while sign-up and signing in using the username and password, or sometimes we verify the user using OTP or a Token verification using email verification or a phone number verification, so we send OTP or a Token in email or sms to verify the genuine user.

We generate a random number and send this number or a token to the user’s email or entered number and also store that generated random number in our table in the database.

When the user enters the generated OTP, we verify and get user information from the table using the OTP request parameter, and after that step, we need to empty and delete that OTP after successful verification, so we update the otp column as a NULL value using the Eloquent in the Laravel application.

Let’s update the database table record as NULL using Laravel Eloquent

Web Route:

<?php

use Illuminate\Support\Facades\Route;


 Route::get('/verify-otp',[App\Http\Controllers\LoginController::class,'otpForm']);
 Route::post('/login',[App\Http\Controllers\LoginController::class,'userOtpVerify'])
    ->name('verify.user.otp');

Blade File:

@if (session()->has('error'))
<div class="alert alert-warning">
   {{ session('error') }}
</div>
@endif
<form action="{{ route('verify.user.otp') }}" autocomplete="off">
   @csrf
   <div class="col-lg-12">
      <input type="text" class="form-control" name="otp" placeholder="Enter otp">
      <span class="text-danger">@error('otp'){{ $message }}@enderror</span>
      <div class="w-100 mt-3">
         <button type="submit" class="btn btn-warning">Submit</button>
      </div>
   </div>
</form>

Controller:

    #user otp verification
    public function otpForm()
    {
       return view('auth.user.otp');
    }

    public function userOtpVerify(Request $reqest)
    {
        $verifyOtp = User::select('otp')->where('otp',$request->otp)->first();
        if ($verifyOtp) {
            $verifiedUser = User::where('otp', $verifyOtp->otp)->update(['otp' => NULL]);
            dd('success');
        }
        else {
            session()->flash('error', 'OTP is expired or invalid.');
        }
    }

I hope that this article helped you learn How to update particular column values in Laravel, with the help of the Laravel eloquent. You may also want to check out our guide on Check users from mobile and desktop without a package in Laravel.