Friday, November 13, 2020

Laravel quick commands for Artisan

 Laravel utilise Artisan to help make working with development faster. This allow programmers to focus more on the development than the management of packages and some repeated workflows.



Here I list the top few commands that I use;

For scaffolding

  • make:controller
    • Create a controller file in app/Http/Controllers
    • php artisan make:controller ArticleController
  • Create a default model
    • php artisan make:model Article
  • Create a model along with migration and factory
    • php artisan make:model Article -crmf
  • Create migration file
    • php artisan make:migration create_articles_table
  • Process all the migration file that haven't been run before
    • php artisan migrate
  • Create Seeder file
    • php artisan make:seeder CustomerTableSeeder
  • Process seeder file
    • php artisan db:seed
    • php artisan db:seed --class=CustomerTableSeeder
  • List command help
    • php artisan list
  • Routing
    • php artisan route:list
  • Debugging
    • php artisan tinker
  • Front-end user interface
    • php artisan ui bootstrap|vue|react
  • Remove Front-end user interface
    • php artisan preset none
  • Create front-end with user authentication
    • php artisan ui vue --auth
  • A stand alone server start, maintenance mode, normal mode
    • php artisan serve
    • php artisan down
    • php artisan up
  • Refresh laravel optimisation files
    • php artisan dump-autoload

Tuesday, November 10, 2020

Laravel and logging of errors

During the course of development, there are many tools and approaches to debug a programme. In Laravel there is a default logging that supports RFC 5424 and here is how to apply it in a Laravel 7 based application.

As a point of reference, this article is for default Laravel 7 on a Apache2 webserver. Which means, if PHP-FPM is used, check the webserver log files. Additionally, there are many more advanced logging by Laravel, for example use of Rollbar, if you have time to explore.

What is Laravel logging? 

Withing Laravel framework, its config/app.php determines classes that does logging. Monolog logging library is used to provide the classes that can be called from the framework. Its flexibility allows logging across different files and even disk. 

The default logging uses a channel known as stack and is configured as

 'default' => env('LOG_CHANNEL', 'stack'),

This provides logging in several levels of severity that range from in the following order;

  • info
  • debug
  • notice
  • warning
  • error
  • critical
  • emergency

Context data is provided for a more consistent format as described in PSR-3.

How to enable default logging? 

Edit the file .env and have this line to enable debug mode;

APP_DEBUG=true

Where is the default logging done? It all in the file laravel.log found in <project_folder>/storage/logs. Ensure the application have correct permission to write app/storage and all its subfolders. When using Selinux, also ensure its allowed to write.

When is logging done?

This can be done practically any where, such as within the controller, views and any other executable.

How to do the logging? 

Taking that you have by now enabled debug mode.

Lets look at how its done within a Controller.

  1. Ensure top of the controller file contains the line
    • use Illuminate\Support\Facades\Log;
  2. Write the code to log your message
    • Log::info('This is where the code runs');

Once when you run the application and the Log::info( ) gets triggered, have a look at the end of file storage/logs/laravel.log to find the message.

Will consider to write further on formatting of the log in order for logging tools to pull data and generate reports or monitoring

Wednesday, November 4, 2020

Laravel ErrorException file_put_contents

After working on any Laravel project for a period of time, its possible that you would change the project folder to another location or drive. Lets say I have a Laravel project called employeeManagement in C:\users\tboxmy\employeeManagement and I make a copy of this to Z:\tboxmy\workspace\project while renaming the project name. 


 

Problem

For some strange reason, the pages are not loading properly and the following error appears.

ErrorException

file_put_contents(Z:\tboxmy\workspace\project\storage\framework/sessions/c34niIDr5qyV8Fyf0qXFLXSF1IlOv3yOulSC9sHI): failed to open stream: No such file or directory 

I have also noticed that the same problem causing that ErrorException SOMETIMES doesn't appear but it is actually loading pages from the original folder location. So when I edit my codes in Z:\tboxmy\workspace\project the changes just doesn't appear in the application. To determine this, just try to rename the original project folder and this error WILL appear.

There are 2 aproches to solve this, which does the same thing.

Solution 1

Step 1

Open a command line and ensure you are in the same folder as the project, in this case Z:\tboxmy\workspace\project\
 

Step 2

Type the command 

php artisan config:cache 
 
 

Step 3 : Update the cache in all installed plugins/libraries

composer install
npm install
npm run dev

Solution 2

Step 1

Using a file manager, or at the command prompt, open the project folder and go to the folder bootstrap\cache
In this case Z:\tboxmy\workspace\project\bootstrap\cache
 

Step 2

Delete the file config.php

Repeat Step 3 above.

Blog Archive