How to Create Model in Laravel 8?
Tags: laravel, php, mysql, tutorial
Published: 2022-04-19 03:09:03

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
php artisan make:model Employee
```

![create-model-in-laravel](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/create-model-in-laravel-8-0PVGu66h5Ax980.webp?ctm=1632828780)

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

**app/Models/Employee.php**

```php
<?php

namespace App\Models;

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

class Employee extends Model
{
    use HasFactory;
}
```

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
<?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;
}
```

Additional Options to Create Model in Laravel Application


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

```php
php artisan make:model Employee --migration
```

Generate model with Factory.

```php
php artisan make:model Employee --factory
php artisan make:model Employee -f
```

Generate model with Seeder.


```php
php artisan make:model Employee --seed
php artisan make:model Employee -s
```

Generate model with Controller.

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

Generate model with Policy.

```php
php artisan make:model Employee --policy
```

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

```php
php artisan make:model Employee -mfsc
```

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


```php
php artisan make:model Employee --all
```

Generate a pivot model.

```php
php artisan make:model Member --pivot
```

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 :)