Development Environment
Firstly, background to my common development environment which leads to the content of notes here. This do change frequently based on project requirements.
- PHP v7.3.7 (cli)
- Centos v7.6
- Kernel v3.10.0-957.10.1.el7.x86_64
- Database Mongo v4.2.2
- Database Postgresql v11
- Apache httpd v2.4.6(CentOS)
On file naming convention
Its important to use name of files, classes and variables that distinguish between singular and plural forms. Example if there is a class to manage Customer base information, that class will be named Customer. A corresponding model will be created, called Customer and the migration file will be named Customers. The controller file will be named based on singular form, such as CustomerController and the view folder is named customer.The table in database will be named customers with a unique field named id. If the customer have an assigned identification number, then provide a separate field such as customer_id.
For more details see note.
Command Line
These are common commands used when creating new application with Laravel framework.Here I refer to a customer as having the fields for customer id (customer_id), name (name) and a contact telephone number (tel). Through Laravel, it should generate its own id as a unique key, which will be different from the customer_id.
Example to create a project Sales
composer create-project --prefer-dist laravel/laravel sales
cd sales
Next step is to configure the database for use with the project. This is done with the file .env
This generates class User and the file app/Customer.php
There are variables that can be declared for this class, example;
Another method is to combine one command that generates the model, resource, controller and migration files
php artisan make:model Customer -crmf
Where
cr = resource controller
m = migration
f = factory
This generates the required files for model (app/Article.php and app/Person.php), controller ( app/Http/Controllers/ArticleController.php), migration (database/migrations/yyy_mm_dd_id_create_people_table.php and database/migrations/yyy_mm_dd_id_articles_table.php) and factory ( database/factories/ArticleFactory.php and database/factories/PersonFactory.php).
This generates the file in database/migration/xxx_customers_table.php
Example of file contents;
Create a new Laravel project using Composer
Ensure the Composer pre-requisites have been installed, then at the command prompt issue the composer command to generate the project files.Example to create a project Sales
composer create-project --prefer-dist laravel/laravel sales
cd sales
Next step is to configure the database for use with the project. This is done with the file .env
Create Model
php artisan make:model CustomerThis generates class User and the file app/Customer.php
There are variables that can be declared for this class, example;
- protected $fillable - for visible user input in forms
- protected $hidden - non visible data in forms and display
- protected $cast - change value to another type.
- protected $table - name of table for this class (if different from class name)
- protected $primaryKey - id for the user registering
Another method is to combine one command that generates the model, resource, controller and migration files
php artisan make:model Customer -crmf
Where
cr = resource controller
m = migration
f = factory
This generates the required files for model (app/Article.php and app/Person.php), controller ( app/Http/Controllers/ArticleController.php), migration (database/migrations/yyy_mm_dd_id_create_people_table.php and database/migrations/yyy_mm_dd_id_articles_table.php) and factory ( database/factories/ArticleFactory.php and database/factories/PersonFactory.php).
Migration
Create Migration Table
php artisan make:migration create_customers_table
This generates the file in database/migration/xxx_customers_table.php
Example of file contents;
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('reference');
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->string('descriptions')->nullable();
$table->string('created_by')->nullable();
$table->string('updated_by')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
Controller and route
Create Controller
php artisan make:controller CustomerController
This generates the file in app/Http/Controllers/CustomerController.php
To generate a controller filled with resources;
php artisan make:controller CustomerController --resources
Create Route to Controller Functions
Route::resource('/customers', 'Customer\CustomerController', ['except' => ['create', 'edit']]);
Edit the file route/web.php with above line. This will search for the common functions in the controller and build the required urls. In this case, create and edit is not created.
Display routes
At command prompt, runphp artisan route:list
Start the application
Default Laravel application is started at port 8000 when at the command prompt it is started with;php artisan serve
Or if this is to be accessed from another PC (Example IP 10.1.1.7) in the network.
php artisan serve --host 10.1.1.7
View web application
In a web browser, open URL to your application. In the case web browser is on the same PC as the application.http://localhost:8000/customers/
Random data generator
Generate Random Data with Faker
Edit the file database/factories/CustomerFactory.php..to do in next posting..
Seed
Create Seeder
php artisan make:seeder CustomerTableSeeder
This generates initial data for the application in file database/seeds/CustomerTableSeeder.php
Load data with the command
php artisan db:seed
Or selectively choose
php artisan db:seed --class=CustomerTableSeeder
Add data in seeder file
Example edit file database/seeds/CustomerTableSeeder.php
public function run()
{
//
$customers= [
['customer_id'=>'A1001', 'name'=>'Yamada', 'phone'=>'12342000'],
['customer_id'=>'A1002', 'name'=>'Joe', 'phone'=>'12346543']
];
foreach($customers as $customer){
Customer::updateOrCreate(['customer_id' => $customer['customer_id']],$customer);
}
}
{
//
$customers= [
['customer_id'=>'A1001', 'name'=>'Yamada', 'phone'=>'12342000'],
['customer_id'=>'A1002', 'name'=>'Joe', 'phone'=>'12346543']
];
foreach($customers as $customer){
Customer::updateOrCreate(['customer_id' => $customer['customer_id']],$customer);
}
}
This loads the 2 data into database.
Example seeder file using Factory classes
Edit the seeder file to create sample data.$user = Factory(App\User::class)->create(
['name' => 'Example User','email' => 'manager@example.com',
]);
The DatabaseSeeder file
It is common to have DatabaseSeeder.php listed with specific order of the seeder files to be loaded. This is due to data dependencies between tables and table constrains.This can be edited in the file database/seeds/DatabaseSeeder.php
Example to load our seeder file
public function run(){
if (app()->environment() == 'production') {
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
} else {
$this->call(UsersTableSeeder::class);
$this->call(CustomersTableSeeder::class);
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
}
}
if (app()->environment() == 'production') {
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
} else {
$this->call(UsersTableSeeder::class);
$this->call(CustomersTableSeeder::class);
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
}
}
Project Files or classes are Not Found, but it exist?
When updated or added routes, migration file, seeder file and etc. are not detected OR gives an error similar to "Not Found", its possible to refresh the Laravel Framework to detect the latest files. There are 2 methods and depends on your situation.
1. Using Composer
composer dump-autoload
This executes vendor/composer/autoload_classmap.php which generates or rebuilds the list of project file and classes. Nothing is downloaded.
2. Using Artisan (DEPRECATED as of Laravel 5 and above)
php artisan dump-autoload
Executes composer with optimised flags, to rebuild list of project files then recompile required files. The file composer.json is referred and other dependencies are downloaded as part of this process.
Debugging
Debugging can be done with the use of log files, see Laravel Logging