Laravel set default value if request value is null

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Laravel, being a popular PHP framework, offers various ways to handle data validation, input, and defaults for your application. In this blog post, we'll discuss how to set default values in Laravel migrations using SQLite and provide solutions when certain issues arise. We'll also offer tips on handling nullable fields in the request and migration scripts. Your initial code snippet shows a possible issue with the migration script. You attempt to assign a default value (BELGIË) for the 'country' column during the table creation process, but you later find out that your SQLite database doesn't have this column. The error could be due to an incorrectly defined migration or an unrelated aspect of the application. To tackle this issue, let's address each part separately: 1. Migration Script: If the 'country' column is not present in your SQLite database, you can either remove the default value from the migration script or create the missing table column afterward. You can update the schema using the following code:
Schema::create('customers', function (Blueprint $table) {
       ->$table->string('country')->nullable();
            $table->id();
            $table->string('name');
            ...
   });
2. Request Validation: The request controller's validation rules currently do not explicitly define the 'country' field as nullable. If you want this field to be optional in your request, update its definition in the request class:
 /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
       ...
       'country' => 'nullable|string|min:2|max:255',
   }
3. Database Migration: To ensure a smooth transition between the initial table structure and the updated one, you can execute your migration scripts sequentially or add conditional logic within your migrations to alter existing columns only if they don't exist yet. This will guarantee consistency in your database structure. 4. Controlling Data Input: When handling data input from users, it's essential to use proper validation mechanisms to avoid unexpected or malicious data being passed to the request controller. For example, you can add a validation rule for 'country' that only accepts ISO country codes (e.g., 'BEL', 'NL') in your migration script:
 Schema::create('customers', function (Blueprint $table) {
       ->$table->string('country')->nullable()->default(CountryEnum::BE_BELGIUM)->after('id'),
            ...
   });
5. Data Migration: When you need to migrate your data from an external source or import existing records, it's best to use dedicated procedures that can handle and manage the process efficiently. Laravel provides various tools like Data Transfer Seeds to automate this task for you. If you require specific logic or business rules, write custom migration scripts tailored to your needs. By following these guidelines and examples, you should be able to successfully set default values in your Laravel migration using SQLite and ensure consistent data handling across the different parts of your application. Remember that proper testing, documentation, and version control are crucial to maintaining a stable system as your project grows. Happy coding!