How to add Default Eloquent Model Values on Laravel 8?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-add-default-eloquent-model-values-on-laravel-8

In this post, I will share an example of how to add default eloquent model v…


This content originally appeared on DEV Community and was authored by codeanddeploy

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-add-default-eloquent-model-values-on-laravel-8

In this post, I will share an example of how to add default eloquent model values on Laravel 8. If you want to add a default value of your model field value that is automatically added every time you save a record.

This is one of the cool features of Laravel that can add a default value when we create a model instance. This is very useful to help to cleaner our code since we don't need to pass extra parameters to our Model but it appends automatically.

With the example below create your migration first but you can skip it.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('description')->nullable();
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Using the $attributes property you can define default values to your model.

Here is the example below to add default eloquent model values.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //...

    /**
     * The model's default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'title' => 'Default Title',
    ];

    //...
}

Now let's test our code with default and without.

In our routes/web.php we added the code below. This is for testing purposes only.

<?php

use App\Models\Post;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/', function () {

    Post::create([
        'body' => 'post body'
    ]);

    Post::create([
        'title' => 'Manually title',
        'body' => 'post body'
    ]);

    $posts = Post::all();

    foreach($posts as $post) {
        echo 'title: '. $post->title . ', body: ' . $post->body . '<br>';
    }
});

As you can see in the first model creation we only pass the body attribute with value but in the second model creation, we included the title and body with each value.

Here is the result below:

how-to-add-default-eloquent-model-values-on-laravel-8

As you can see the first line result using the default value defined from our model. And the second was added manually.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-add-default-eloquent-model-values-on-laravel-8 if you want to download this code.

Happy coding :)


This content originally appeared on DEV Community and was authored by codeanddeploy


Print Share Comment Cite Upload Translate Updates
APA

codeanddeploy | Sciencx (2022-04-19T04:02:39+00:00) How to add Default Eloquent Model Values on Laravel 8?. Retrieved from https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/

MLA
" » How to add Default Eloquent Model Values on Laravel 8?." codeanddeploy | Sciencx - Tuesday April 19, 2022, https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/
HARVARD
codeanddeploy | Sciencx Tuesday April 19, 2022 » How to add Default Eloquent Model Values on Laravel 8?., viewed ,<https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/>
VANCOUVER
codeanddeploy | Sciencx - » How to add Default Eloquent Model Values on Laravel 8?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/
CHICAGO
" » How to add Default Eloquent Model Values on Laravel 8?." codeanddeploy | Sciencx - Accessed . https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/
IEEE
" » How to add Default Eloquent Model Values on Laravel 8?." codeanddeploy | Sciencx [Online]. Available: https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/. [Accessed: ]
rf:citation
» How to add Default Eloquent Model Values on Laravel 8? | codeanddeploy | Sciencx | https://www.scien.cx/2022/04/19/how-to-add-default-eloquent-model-values-on-laravel-8/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.