In this post, we will explain you Laravel set default value in model example. let’s discuss about how to set default value in model Laravel. We will look at an example of laravel model set default value and set migration file add default value.
You can use this example with Laravel application. In this example I will show you how to set the default eloquent model value in laravel. You can use $attributes variable to set default value of model.
We will create posts table with title and body fields. We will set default title from model. Let’see the below example.
Laravel set default value in model example
Create posts table migration
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 code on migration file.
Migration file code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
Then, simply run migration command to run.
php artisan migrate
Create Post model
Let’run below command to run create model.
php artisan make:model Post
Here, we will create Post model and we will set title fields default value using $attributes. So let’s add the below code in your model.
<?php
namespace App\Models;
use App\Enums\PostStatusEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ['title', 'body'];
protected $attributes = [
'title' => 'Default title',
];
}
Create Route add web.php
In this is step we need to create one route for testing purpose.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('post', [PostController::class, 'index']);
Create PostController
In this step, we need to create PostController with index() to create posts and display posts. Let’see below controller code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index(Request $request)
{
Post::create([
'body' => 'Post Body Added'
]);
Post::create([
'title' => 'Manual Title Added',
'body' => 'Post Body Added'
]);
$posts = Post::latest()->orderByDESC('id')->get();
return view('posts', compact('posts'));
}
}
Create blade file
In this step, we will create posts.blade.php and display all posts on it.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Laravel set default value in model example - webstuffsolution.com</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Laravel set default value in model example - webstuffsolution.com</h1>
<table class="table table-bordered data-table">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
@foreach ($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->body }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
Run Laravel app:
All the required steps have been done then run Laravel app.
php artisan serve
2. Direct default value set in Migration file
Defualt values can be set at the database schema level using migrations. The default value is applied even if no value is provided during data insertion.
Example in Laravel Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title')->default('Default title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
I hope this tutorial help you.