Create Model in Laravel using command

Posted on

This is an article which is used to create a model manually on Laravel framework based PHP application. It is actually an easy task and it can be done in several ways which is manually or created by the help of artisan tool.

The process of creating model in Laravel framework based PHP application is specified as follows :

1. Create model manually

The process of creating model manually is done by using editor tool ‘vim’ for an example. It is a text editor tool available among the other which can be utilized to create a file. Below is the syntax usage of ‘vim’┬átext editor tool to create a file to be used as model :

touch ModelFileName.php
root@hostname:/var/www/html/laravel-project/app# touch MyModel.php 

After successfully create the file, editing the newly created file can be done by using the following command :

vim MyModel.php

Below is the execution of the above command in a real situation :

root@hostname:/var/www/html/laravel-project/app# vim MyModel.php

2. Create model by using php artisan tool.

Another way which can be executed is by using artisan tool. The command pattern is shown as follows :

php artisan make:model MyModelFile
root@hostname:/var/www/html/laravel-project# php artisan make:model MyModel
Model created successfully.                                                                                                                                                                                                       
You have new mail in /var/mail/root

After successfully created the model file named MyModel. Search trough the Laravel project directory to find out the location of the newly created model. It can be done by executing the following command :

find [searching-starting-point-path] -name "*text-pattern-for-searching*"

Description : 
find : The command which is used to search files in a directory hierarchy
[searching-starting-point-path] : The additional parameter to inform the initial location for searching
-name : The additional parameter used for passing the string pattern of searching
"*text-pattern-for-searching*" : The string pattern used for searching.

Below is the example of the execution :

root@hostname:/var/www/html/laravel-project# find . -name "*MyModel*"
root@hostname:/var/www/html/laravel-project# cd app/
root@hostname:/var/www/html/laravel-project/app# ls
Console  Exceptions  Http  Providers  MyModel.php  User.php
root@hostname:/var/www/html/laravel-project/app# ls -al
total 32
drwxrwxr-x  6 apache apache 4096 Feb  7 18:00 .
drwxrwxr-x 13 apache apache 4096 Feb  4 19:31 ..
drwxrwxr-x  2 apache apache 4096 Sep 20 20:38 Console
drwxrwxr-x  2 apache apache 4096 Sep 20 20:38 Exceptions
drwxrwxr-x  4 apache apache 4096 Sep 20 20:38 Http
drwxrwxr-x  2 apache apache 4096 Sep 20 20:38 Providers
-rw-r--r--  1 root   root   102  Feb  7 18:00 Server.php
-rw-rw-r--  1 apache apache  511 Sep 20 20:38 User.php

Since the process of creating the model above is done using ‘root’ account, don’t ever forget to change the ownership of the file which has been created.

root@hostname:/var/www/html/laravel-project/app# chown -Rv www-data.www-data Server.php 
changed ownership of 'Server.php' from root:root to www-data:www-data
root@hostname:/var/www/html/laravel-project/app# cp User.php NetworkDevice.php
root@hostname:/var/www/html/laravel-project/app# chown www-data.www-data NetworkDevice.php 

Below is the minimal content of a newly created model using artisan tool which is also become the base template for manually created model :


namespace App;

use Illuminate\Database\Eloquent\Model;

class MyModel extends Model

3 thoughts on “Create Model in Laravel using command

Leave a Reply