DEV Community

Cover image for How to Create Model in Laravel 8?
Code And Deploy
Code And Deploy

Posted on

How to Create Model in Laravel 8?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-create-model-in-laravel-8#kDJtXEkWUdNnyQ39IUCm4WwqQ

In this post, I will show you an example of how to create a model in the Laravel 8 application. I will give you an example so that it will easier to you to follow and understand. Laravel has a built-in command to run that will generate the Model to your Laravel 8 application.

After following this tutorial for creating a model in Laravel using the artisan command surely it will be easier for you from now on. Laravel artisan can run in any operating system like Ubuntu and CMD in Windows.

What is a Model in Laravel Application?

The Model is a PHP class that performs business logic and database manipulation like - retrieving, inserting, updating, and deleting data.

Create Model in Laravel

In the Laravel application we just simply run a command to create a model in Laravel 8. See the following example below:

php artisan make:model Employee
Enter fullscreen mode Exit fullscreen mode

create-model-in-laravel

Now you will see the generated model in your Models directory:

app/Models/Employee.php

<?php

namespace App\Models;

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

class Employee extends Model
{
    use HasFactory;
}
Enter fullscreen mode Exit fullscreen mode

Setup Model Properties

Now let's, add protected $table = ""; and protected $fillable = []; to our model and defined each values.

protected $table: Define model table name

protected $fillable: Fields that are mas-assignable

See below on how to setup:

<?php

namespace App\Models;

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

class Employee extends Model
{
    /**
     * @var string $table
     */
    protected $table = 'employees';

    /**
     * @var array $fillable
     */
    protected $fillable = [
        'field_name_1',
        'field_name_2'
    ];

    use HasFactory;
}
Enter fullscreen mode Exit fullscreen mode

Additional Options to Create Model in Laravel Application

If you would like to generate database migration then apply this command:

php artisan make:model Employee --migration
Enter fullscreen mode Exit fullscreen mode

Generate model with Factory.

php artisan make:model Employee --factory
php artisan make:model Employee -f
Enter fullscreen mode Exit fullscreen mode

Generate model with Seeder.

php artisan make:model Employee --seed
php artisan make:model Employee -s
Enter fullscreen mode Exit fullscreen mode

Generate model with Controller.

php artisan make:model Employee --controller
php artisan make:model Employee -c

Enter fullscreen mode Exit fullscreen mode

Generate model with Policy.

php artisan make:model Employee --policy
Enter fullscreen mode Exit fullscreen mode

Generate model with migration, factory, seeder, and controller.

php artisan make:model Employee -mfsc
Enter fullscreen mode Exit fullscreen mode

Generate model with migration, factory, seeder, policy, and controller.

php artisan make:model Employee --all
Enter fullscreen mode Exit fullscreen mode

Generate a pivot model.

php artisan make:model Member --pivot
Enter fullscreen mode Exit fullscreen mode

That's it. Now you have an idea already on how to create a model in Laravel 8 using the artisan command. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-create-model-in-laravel-8#kDJtXEkWUdNnyQ39IUCm4WwqQ if you want to download this code.

Happy coding :)

Top comments (0)