Hello Geek, In Laravel, you can set the default value for a model’s attribute by defining a default value in the migration file that creates the table for the model.
You can use the $table->string('attribute_name')->default('default_value');
syntax within the up()
method of the migration to define the default value.
Here’s an example of how you might define a default value for a model’s status
attribute:
Install Laravel
composer create-project laravel/laravel example-app
Create Migration
php artisan make:migration create_posts_table
Then, add the following code to the migration file.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('status')->default('active');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
Then, simply run migration command to run.
php artisan migrate
Create Model
To create a model in Laravel, you can use the following steps:
- Open the terminal and navigate to the root directory of your Laravel project.
- Run the following command to generate a new model:
php artisan make:model ModelName
- This will create a new model file in the ‘
app
‘ directory with the nameModelName.php
. - Open the model file and add the following code to define the table name for the model:
protected $table = 'table_name';
- If you want to define relationships between models, you can use the ‘
hasOne
‘, ‘hasMany
‘, ‘belongsTo
‘, or ‘belongsToMany
‘ methods. - Finally, you can use the model to perform database operations, such as creating, reading, updating, and deleting records.
Example:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
public function posts()
{
return $this->hasMany('App\Post');
}
}
Create Route
This step requires us to create one route for testing. Let’s add the following route to the web.php file.
<?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('demo-posts', [PostController::class,'index']);
Create Controller
In this step, we’ll make a PostController with index() to create and display posts.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
Post::create([
'body' => 'Post Body Added'
]);
Post::create([
'title' => 'Manually Title Added',
'body' => 'Post Body Added'
]);
$posts = Post::latest()->get();
return view('posts', compact('posts'));
}
}
Create Blade File
<!DOCTYPE html>
<html>
<head>
<title>How to Set Default Value from Model in Laravel? - ItSolutionStuff.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>How to Set Default Value from Model in Laravel? - ItSolutionStuff.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:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/demo-posts
All the best nerd!