Introduction
This is an article about how to create a model in Laravel-based project or application. Actually, it is very important to have a model in the Laravel-based project or application. It acts as a data container which is an abstraction layer for connecting between the database and the application. So, it will mirror the data structure in the table for retrieving and storing the data from and to the application. In this article, it will show how to create it.
How to Create a Model in Laravel
In order to create a model in Laravel-based project or application, just follow the sequence below :
-
-
It is obvious that the first step is to have a Laravel-based project or application. Feel free to look in other article as a reference, but there is also one in this link. It is an article with the title of ‘How to Create a Laravel Project using Composer in Microsoft Windows’.
-
Following after, just try to run the command line interface.
-
Next, access the root path of the Laravel-based project or application. In this case, the path is in ‘C:\programming\language\php\laravel\crud’.
-
In that path, execute the following command pattern :
C:\programming\language\php\laravel\crud> php artisan make:model Category Model created successfully. C:\programming\language\php\laravel\crud>
-
Afterwards, check the model generated from the previous command execution. In this case, the location for the Laravel-based project or application exists in ‘C:\programming\language\php\laravel\crud’. So, the model will be exist in the folder of ‘app\Models’ where the full path of it will be ‘C:\programming\language\php\laravel\crud\app\Models’. So, this is the actual content of the location or path :
C:\programming\language\php\laravel\crud\Models>dir Volume in drive C is Windows Volume Serial Number is E003-3593 Directory of C:\programming\language\php\laravel\crud\Models 08/03/2021 09:30 PM <DIR> . 08/03/2021 09:30 PM <DIR> .. 08/04/2021 05:49 AM 959 Category.php 07/13/2021 09:12 PM 832 User.php 6 File(s) 3,014 bytes 2 Dir(s) 9,007,104 bytes free C:\programming\language\php\laravel\crud\Models>
Furthermore, the content of the file with the name of Category.php exist as follows :
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Test extends Category { use HasFactory; }
-