Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Insert null data if the request value is empty in Laravel

Insert null data if the request value is empty in Laravel

In this tutorial, I will give you an example of the “How to insert null data if the request value is empty in Laravel“, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

Let’s assume we have a field name email in our table and the field is not nullable and the email field is not required so we didn’t use any frontend and backend validation for email, this will show an error on the form submit.

Our request email is NULL and we are assigning that null value when creating a new row, either we have to use nullable to insert a null value or we have to set a value when creating.

There are two ways to Handle Null Values in the Laravel application.

Handle Null Value from Database

$table->string('email')->nullable();

Handle Null Value in Controller

   public function store(Request $request){
        $reqdata = new Modelname();
        $reqdata->nama  = $request->name;
        $reqdata->email = isset($request['email']) ?  : NULL;
        $reqdata->save();
        return redirect()->back()->with('success','Data Saved Successfully!');
    }

In this article, we learned “Insert Null data if the request value is empty in Laravel”, I hope this article will help you with your Laravel application Project.

Also Read : Autocomplete search using Ajax in Laravel.