Saturday, 31 October 2020

Create Custom Laravel Helpers

Laravel Provider a lot of helper functions for Arrays & Objects, Strings, Fluent Strings, Miscellaneous, and the most used helper function dd().

Step 1: Define Your Helper Functions

To define helpers in laravel create a php file and define a class with methods in it
and place the file at app/helpers.php folder in your laravel application.For example

<?php
namespace App\Helpers;

class MyCustomHelper
{
    public static function formatDateTime($dateTime)
    {
        return $dateTime->format('Y-m-d H:i:s');
    }
}

Friday, 2 October 2020

Commonly Used Laravel Artisan Console Commands

Commonly Used Laravel Artisan Console Commands

Laravel, the widely acclaimed PHP framework, enhances web development with its elegant syntax and comprehensive features. Among its powerful tools, the Artisan Console stands out, offering developers a suite of command-line operations to streamline project tasks. This brief article explores the commonly used Laravel Artisan Console commands, essential for efficient development workflows.

Most of the time laravel developer use allot of different laravel artisan commands, to clear caches
to run development server , creating controllers, models, and other useful artisan options.
    Key Laravel Artisan Commands

  1. php artisan cache:clear  Clear the application's cache, improving performance and ensuring fresh data is served to users.
  2. php artisan config:cache Clear the application configuration cache
  3. php artisan route:clear  Clear Application route cache
  4. php artisan view:clear  Clear Application View cache
  5. php artisan serve  Running development server
  6. php artisan serve: Launch a development server, making your application accessible at a localhost URL. This command is particularly useful for quick testing and development.
  7. php artisan make:migration create_table_name_table: Generate a new migration file for creating a database table. Migrations ensure database structure consistency across different environments.
  8. php artisan db:seed: Populate your database with seed data, an invaluable feature for testing and development purposes.
  9. php artisan tinker: Enter a REPL (Read-Eval-Print Loop) environment that allows you to interact with your Laravel application in a command-line interface, facilitating debugging and experimentation.
  1. Above list of commonly used artisan console commands will work with laravel 6,7,8 and 9, 10 and almost with all versions of laravel framework.

Thursday, 1 October 2020

Laravel 6 and 7 Authentication

Laravel 6 and 7 Authentication

Laravel simplifies authentication implementation, providing robust, out-of-the-box support for common authentication tasks. Laravel 6 introduced Laravel UI, a separate package, to manage authentication scaffolding, whereas Laravel 7 continued to refine these features, ensuring a smoother developer experience.

Moreover make:auth command no longer exists in Laravel 6 and Laravel 7.So if we want to use laravel auth in laravel 6/7 applications then we need to use the Laravel UI package(laravel/ui).

1) Install Laravel UI (For laravel 6)

Use Composer to install the Laravel UI package and generate the auth scaffolding.


To install composer package laravel/ui run
composer require laravel/ui

After installing of above package, we will have a couple of new commands available.To see new commands run.
php artisan ui --help

ui:auth Command

To generate authentication views we need to run following artisan command.
php artisan ui:auth
It will generate 
  1. HomeController
  2. auth routes
  3. auth views( registration, forgot password, login, reset password views )
  4. app.blade.php
To generate only views in your existing application then you need to add --views.

For example:
php artisan ui:auth --views
For further details please visit official documentation:

Laravel csrf token mismatch for ajax post request

Error "CSRF Token Mismatch" commonly occurs if tokens do not match in both sessions and sent , and received requests.CSRF token he...