Laravel Set Default Value using Model
Hello Artisan, today I'll show you how to set default for a column from your model. We often used to set default value from in our migration file but sometimes for our custom requirement or for any other purposes we need to modify, but at the end of an application cycle it's quite risky to modify/messy to customize a database column. And that's why we'll use our model classes to set the default value for a column. And it' going to be quite easy process rather than making a migration file and then perform a migrate command. So, no more talk, let's see how we can easily complete our todays' task.
Note: Tested on Laravel 9.19.
Table of Contents
Create and Setup Controller
In the very first step, we'll create a controller, and we'll write our necessary codes/logics.
Now fire the below command in terminal
php artisan make:controller UserController
It'll create a file under app\Http\Controllers called UserController.php. Open the file and replace with below codes.
<?php
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller
{
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
{
User::create([
'name' => 'Tanvir',
'email' => '[email protected]',
'password' => bcrypt(12345678),
]);
User::create([
'email' => '[email protected]',
'password' => bcrypt(12345678),
]);
return view('index', [
'users' => User::all(),
]);
}
}
Create and Setup Route
Let's put the below route in web.php
<?php
use Illuminate\Support\Facades\Route;
Route::resource('users', \App\Http\Controllers\UserController::class)->only([
'index'
]);
Create and Setup View File
Now we we'll create a blade file where we can setup our view where we can see list of users. So, let's create a view file called index.blade.php under resources/views.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<!-- Popper JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-md-12">
<h4 class="text-center">Set Default Value in a Column By Model</h4>
<table class="table mt-5">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Created At</th>
</tr>
</thead>
<tbody>
@foreach($users as $key=> $user)
<tr>
<th scope="row">{{ $key+1 }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Setup Model
Open User.php and replace with below codes.
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $attributes = [
'name' => 'Default Name',
];
}
Here we use $attributes property for set the default value for a specific column.
Output
And finally, we're ready with our setup. It's time to check our output. Now go to http://localhost:8000/users, If everything goes well (hope so) you can find a below output.
That's it for today. Hope you've enjoyed this tutorial. Catch me in the comment section if anything goes wrong. You can also download this tutorial from GitHub. Thanks for reading.