In this post we have explain of laravel set default value in model. Let’s discuss about how to set default value in laravel model. Default value in Laravel models is a useful feature to ensure that certain field have predefined values when a new record is created.
How to set Laravel default value in model?
When working with Laravel eloquent models, may want to assign default values to certain attributes if they are not provided during the model’s creation. Set default value to avoids database errors. In this post, we will explore methods to set default values in Laravel models.
In this example, I will explain Laravel set default value in model. You can use $attributes variables to set default value of model.
Step 1: Install Laravel
First of all we need to create fresh Laravel application using below command.
composer create-project laravel/laravel testing
Step 2: Create Migration
Here, we will create migration for posts table, so let’s run the below command and run migration.
php artisan make:migration create_posts_table
Then, let’s put the below migration table code in migration file.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Then, simply run migration command to run.
php artisan migrate
Read also: How to use unique validation in Laravel?
Step 3: Create Model
Here, we will create Post model and we will set title fields default value using $attributes.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title', 'body'
];
protected $attributes = [
'title' => 'Defult post title'
];
}
Step 5: Create Route
In this step we need to create one route for testing purpose. Add route in web.php
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('testing', [PostController::class, 'index']);
Step 5: Create Controller
In this step, we need to create PostController with index() to create posts.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request)
{
Post::create([
'body' => 'Post body 1',
]);
Post::create([
'title' => 'Manually addded title',
'body' => 'Post body 2'
]);
$posts = Post::latest()->get();
dd($posts);
}
}
Output:
array:2 [▼ // app\Http\Controllers\PostController.php:23
0 => array:3 [▼
"id" => 27
"title" => "Defult post title"
"body" => "Post body 1"
]
1 => array:3 [▼
"id" => 28
"title" => "Manually addded title"
"body" => "Post body 2"
]
]
I hope this tutorial help you.