Category: Laravel Tutorial

  • Laravel Tutorials

    Laravel is a PHP application framework created by Taylor Otwell
    It is a free and open source framework that enables rapid development of web applications

    Advantages of Laravel

    Laravel offers several advantages for web developers
    It has expressive, elegant syntax, follows model-view-controller design pattern that is useful for web application development

    Thus the web application can be developed rapidly, securely and becomes more scalable.

    Features of Laravel:

    • MVC Architecture Support
    • Object-oriented
    • Modularity
    • Testability
    • Routing
    • Configuration Management
    • Query Builder and ORM
    • Schema Builder
    • Template Engine
    • E-mail
    • Authentication
    • Redis
    • Queues
    • Event and Command Bus
    • Template Engine
    • Eloquent ORM
    • Security
    • Artisan
    • Libraries & Modular
    • Easy Installation
    • Data Migration
    • Multiple File System
    • Scheduling Tasks
    • Testing
    • Caching

    Pl. contact us for developing a Laravel based custom web application.

    Resources:

    Laravel Website – https://laravel.com/
    Laravel Source Code – https://github.com/laravel/laravel

  • Upgrade Laravel to 8.x

    In this section, here I’ll discuss how to upgrade Laravel7.x to new Laravel8.x.

    PHP version:

    For upgrading to laravel8.x you must have PHP version 7.3.0 (Must needed 7.3 or higher php version).

    Upgrade Dependencies

    For upgrading your project open file composer.json and edit/update the following dependencies version:

    guzzlehttp/guzzle to ^7.0.1
    facade/ignition to ^2.3.6
    laravel/framework to ^8.0
    laravel/ui to ^3.0
    nunomaduro/collision to ^5.0
    phpunit/phpunit to ^9.0
    Note: This is important, If you’re using any packages in your laravel project so go to the GitHub or their website and check their upgrade guide or latest version supported to Laravel8.x or not. If package supported the latest laravel version then update the dependencies like above one.

    For example,
    In my project I’ve used the laravel telescope with version 3.2 => “laravel/telescope”: “^3.2”,
    So, upgrade to this into new one check the latest version of this package and update this, “laravel/telescope”: “^4.1.0”,

    After setup all of these dependencies, run these commands:

    • Open your terminal and run the composer update command for upgrade the project into new one.
    • After completing the composer update command run this command next:
      composer require laravel/legacy-factories
  • Laravel Media Library

    Why Media Library?

    Medialibrary is a Laravel5.5+ package that can associate all sorts of files with the Eloquent models. And it provides a simple, fluent API to work with.

    Features of Media Library

    • It can directly handle your uploads
    • It can store some large files on another filesystem because the storage of the files is handled by Laravel’s Filesystem, so you can plug in any compatible filesystem.
    • The package can generate derived images such as thumbnails for images, video’s and pdf’s.

    Requirements for Media Library v7/

    • PHP version should be 7.1+
    • Laravel version requires 5.5.0+
    • MySQL required 5.7 or higher

    If you have an older version of PHP and Laravel, then you should use a media library with a lower version.

    In this article, I have used the spatie/laravel-medialibrary package. Now, I will show you how to install the media library package in Laravel with the few steps. And with this, I will show you a simple example of this package.

    1. Installation & Setup of Media Library

    Step1: Install Laravel

    First install the Laravel project using composer commands:
    composer create-project –prefer-dist laravel/laravel LaravelMedia

    Step2: Install Medialibrary Package

    After this, go to your Laravel project directory and run this composer command:
    composer require “spatie/laravel-medialibrary:^7.0.0”
    This command will install medialibray package in your Laravel project.

    Step3: Database configuration

    First, create the database for this media library package. Open phpMyAdmin and create database like this:
    Next we need to configure database with your Laravel project. So, open .env file and configure like this:

    Step4: Publish & Run migration

    Next, we need to publish and run the migration for the medialibrary. Run this artisan command:
    php artisan vendor:publish –provider=”Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=”migrations”
    This command will create media table for your media library. You can see the migration table in your project database/migrations/create_media_table.php directory like this:
    Next, run the artisan migrate command: php artisan migrate

    Step5: Publish configuration file

    Next, we need to publish the configuration file for media library. So run the command in you terminal:
    php artisan vendor:publish –provider=”Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=”config”
    This command will create configuration file for media library and create medialibrary.php file at your config directory.
    Default content of this config/medialibrary.php file:

    2. Basic Usage:

    Preparing your model
    To associate media with a model, the model must implement the following interface and trait like this:
    For example, I have use this trait in our default User.php model like this:
    Make Basic Layouts and Routes for media library
    Create authentication for your Laravel project. Run the make:auth artisan command in your terminal:
    php artisan make:auth
    Next, open default resources/views/layouts/app.blade.php file and add write code for adding an extra profile link.
    You will get output like this:
    Create view file for media library:
    Create profile.blade.php view file under resources/views/ directory and write a line of code for your profile views file.
    Define route for views and controller in routes/web.php file like this:
    After this register the user and navigate the URL 127.0.0.1:8000/profile in your browser, you will get output like this:
    Associating files:
    For associating the file, create a controller and define media function in your controller.
    Create resource controller using make artisan command: php artisan make:controller AvatarController –r. This will create a controller file at app/Http/Controller/ directory.
    Next, open AvatarController.php file and associate the file in different function like this:
    Write code in index function like this:
    Write code in store function like this:
    Defining Media Collections:
    A media collection can be more than just a name to group files. By defining a media collection in your model you can add certain behavior collections.
    To get started with media collections add a function called registerMediaCollections to your prepared model like User.php model. Inside that function you can use addMediaCollection to start media collection.
    Open User.php model and define media collection like this:
    Defining Conversion
    You should add a method called registerMediaConversions to your model. In that model you can define the media conversion. Here is an example.
    Open User.php model and define the media conversion:

    3 Results

    Open browser and navigate the URL: 127.0.0.1:8000
    Register the user first: 127.0.0.1:8000/register
    Next, click to profile and upload images, you will get output like this:
    Note:
    • To learn more about this media library package, visit this link:
    https://docs.spatie.be/laravel-medialibrary/v7/introduction/.
    • Source code for this project is available on GitHub: https://github.com/digitalcrm/LaravelMedia.

  • Role Management

    What is Role Management/Permission?
    With the help of tables, we will understand the basic concept of role and permission. What they are actually:
    Role Table:

    S/N

    Role

    Permission

    1

    Admin

    Create,
    Edit, Delete, Users

    2

    Stakeholder

    Create,
    Edit

    Permission Table:

    S/N

    Permission

    Description

    1

    Create

    Grants
    authorization to create any type of record except users

    2

    Edit

    Grants
    authorization to edit any record except users.

    3

    Delete

    Grants
    authorization to delete any record except users.

    4

    Users

    Grants
    permission to display, create, edit, assign user roles, and delete users.

    Role: Basically role represents a group of tasks that a user that is assigned the role is allowed to perform. For example: a user role that allows user to edit their own listing page. And the system admin can be regarded as owner of the system and as such, is permitted to perform all the tasks in the system. Admin has power to create users, delete, assigning role to user and edit products etc…
    Permission: The ability to do actions on your site like viewing, create, delete and edit etc is governed by permissions. Permission grants authorization to a role to perform a specific task. A user must be granted a permission in order to do the corresponding action on the site.
    In this article, with simple demo of role based, we have implemented a role based authentication in Laravel along with its native authentication system. So, just follow the steps and see how the role permission works in Laravel.
    Note: Source code for this is available on GitHub: https://github.com/digitalcrm/Role-Management

    1. Install Laravel Project:

    composer create-project –prefer-dist laravel/laravel RoleManagement “5.8.*”

    2. Database Configuration:

    2.1. Create database: Open phpMyAdmin and create database, like this:

    2.2. Configure with Laravel: After database has been created, next open .env file and add database credentials, like this:

    3. Create Models and Migrations

    3.1. Create a Role model and migration to running this command: php artisan make:model Role –m

    3.2. Create a role_user migration:
    php artisan make:migration create_role_user_table

    3.3. Modify Role migration table: Open database/migrations/create_roles_table.php

    3.4. Modify create_role_user_table migration table:
    Open database/migrations/create_role_user_table.php.

    3.5. Running the migration: After the migration table has been setup you can create the roles and role_user tables by running the migrations: php artisan migrate.

    will create the following tables in your database.

    4. Make Authentication Scaffolding

    Run the artisan command: php artisan make:auth

    5. Database Entries:

    5.1. After running the migration command, now we need to fill the data manually for testing the role permission, So, fill the data in roles and role_users table:
    5.1.1. Open and fill the data manually in roles table:

    5.1.2. Open and fill the data manually in role_user table:

    5.1.3. Also register user in users table using authentication, go to /register url and register the users:

    6. Model Setup

    6.1. Open app/Role.php model and define Many to Many relationship, like this:

    6.2. Open default created app/User.php model file and define Many to Many relationship, like this:

    6.3. Also, open User.php and include these methods which will be used to check if user has a particular role or roles:

    7. Create Controller:

    Now we need two controllers for admin and superadmin, so let’s create:
    7.1. Run the artisan command: php artisan make:controller AdminController

    Open AdminController and add code like this:

    7.2. Run the artisan command: php artisan make:controller SuperAdminController

    Open SuperAdminController and add code like this:

    h4>8. Create View:

    Next, we have to create views files for admin and super admin.
    8.1. Create view for admin: resource/views/admin/home.blade.php

    8.2. Create view for superadmin: resource/views/superadmin/home.blade.php

    9. Define Routes:

    Open routes/web.php and create routes for admin and superadmin:

    10. Create Middleware:

    Next, we need a middleware for our role. So, run the artisan command for creating middleware:
    php artisan make:middleware CheckRole

    Open app/Http/CheckRole.php file and add code:

    11. Register The Middleware:

    Open app/Http/kernel.php file and include role middleware under $routeMiddleware array, like this:

    12. Results:

    Only authorized users can have privileged to access the certain part of your applications:
    Admin Login:

    After admin login if super admin wants access:

    If admin access this:
    Note: For the source code, you can visit the GitHub directory: https://github.com/digitalcrm/Role-Management
    GitHub link:
    Laravel-Media-Library: https://github.com/digitalcrm/LaravelMedia
    Role Management: https://github.com/digitalcrm/Role-Management
    Newsletter Package: https://github.com/digitalcrm/pkg-newsletter
    Form-Package: https://github.com/digitalcrm/Form-Contact-Package

  • Newsletter Package

    In this article, I will show you how to create a Newsletter Package in Laravel and how to use this. You can check the source code from GitHub link: https://github.com/digitalcrm/pkg-newsletter and package in packagist.org link: https://packagist.org/packages/digitalcrm/newsletter

    Creating a Package:

    In this article, I have created a Newsletter package which will manage the newsletter in Laravel. So just follow the steps:
    1. First, install fresh Laravel Project:
    Run command in your terminal: Composer create-project –prefer-dist laravel/laravel PkgNewsletter “5.8.*”

    2. Create a package folder in the root directory and under this create a subfolder newsletter, like this:

    3. Next, for package, we have needed two main files: one composer.json and second is Service Provider file.
    3.1. Creating composer.json file: Select package path in your terminal and run the composer init command which will create the composer.json file in your newsletter package but after running this command, this will ask various question like package name and vendor, description and author etc… So fill this information.

    After completion of init command, you can see composer.json file is created under your package directory. So, open package/newsletter/composer.json file:

    3.2. Creating Service Provider: After setup the composer.json file, then we have needed a Service Provider for your package, so create a “src” folder under package/newsletter/, like this:

    Create NewsletterServiceProvider.php file under this package/newsletter/src/ directory and add code under boot() and register() function for load, publishing and registering the services of packages file.

    4. Modify the composer.json file:
    4.1. Open your package composer.json file and add require field, autoload psr-4 and package discovery in your package/newsletter/composer.json file, like this:

    Open main composer.json file and add your package path in autoload-dev. Because this will tell your Laravel project that there is a package inside the Laravel project.

    4.3. Open your terminal, select the project path and run the composer command: composer dump-autoload. This command will help to recognize the package.

    5. Create files for newsletter package: All required files for packages will be created under the src folder. You can visit the GitHub directory for this source code: https://github.com/digitalcrm/pkg-newsletter/tree/master/src.

    For this package, I have used the Mailchimp for mail services. To know more about Mailchimp visit https://mailchimp.com/. So, I have configured MailChimp in our package, like this;
    src/config/newletter.php

    src/Exception/InvalidNewList.php

    src/NewsletterFacade.php

    src/NewsletterList.php

    src/NewsletterCollection.php

    src/NullDriver.php

    src/Newletter.php

    Note: When using this newsletter package, you should have to integrate Mailchimp (MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID) in your .env file. For getting the API key and List Id, you should have to create an account in Mailchimp first, where you will get your API Key and List Id.

    Upload in packagist.org

    Here with few steps, you can upload your package in packagist.org. Just follow the steps:

    Step1:

    First you need to open packagist.org and create your account using GitHub and sign in:

    Step2:

    After “sign in” in Packagist.org, then you have to upload your package in GitHub repository.

    Step3:

    After uploading the package in GitHub, copy the link address and go to the packagist.org then click on submit and paste the address link:

    Step4:

    Finally, after submitting package, your package has been uploaded and you will get your composer require command for this package.

    How To Use fincrm/newsletter Package:

    Here, I will discuss how to use this newsletter package. Just follow the steps given below:

    Step1:

    First download the Laravel project. Run this composer command:
    composer create-project –prefer-dist laravel/laravel NewsLetterPackage

    Step2:

    After downloading the Laravel project, next run this composer command:
    composer require fincrm/newsletter. This command will download the newsletter package in your Laravel project.

    Step3:

    Publishing the configuration file run:
    php artisan vendor:publish –provider=“Fincrm\Newsletter\NewsletterServiceProvider”
    This command will publish the config file to config/newsletter.php.

    Step4:

    Next, we need view, controller and routes for running the newsletter package. So let’s create a view, controller and routes for this newsletter package.
    View file: Create view file under resources/views/subscriber.blade.php and add code:

    Controller: For creating controller run the command:
    php artisan make:controller NewsLetterController –r.

    This command will create a resource controller under app/Http/Controllers/NewsLetterController.php. Then open this file and add code:

    Finally create routes under routes/web.php.

    Step5:

    Open .env file and integrate MailChimp API_KEY and LIST_ID. You get your own MailChimp API key and List ID from mailchimp.com.

    Step6:

    Navigate the URL: http://127.0.0.1:8000/subs in your browser.
    • Newsletter page:

    • After Submitting

    • Open your mail account and you will get subscription page, like this

  • Form-Package

    this article, I will show you how to create a form package in Laravel and how to use this package. You can check the source code from GitHub directory link: github.com/digitalcrm/Form-Contact-Package.

    1. Create Form Package

    In this article, I have created a contact form package which will send an email and save the contact query in database. So just follow the steps:

    Step1: First, install the Fresh Laravel Project:

    Run the command: composer create-project –prefer-dist laravel/laravel BuildForm

    Step2:

    Create a folder package in the root directory and create a subfolder contact under this package. This package will do a simple thing which is just sending the email and save the contact details and the messages in the database.

    Step3:

    We need at least two things for our form package; one is package.json (composer.json) file and second is service provider (ContactServiceProvider.php). So, run the composer init command which will create the composer.json file in your contact form but after running this command, this will ask various question like package name and vendor, description and author etc… So fill this information.

    • After completion of command, you can see composer.json file is created under your package (contact) directory:

    • Next define your package under main composer.json file and as well as add autoload psr-4 under package/contact/composer.json file which will tell our composer to there is a package inside Laravel Project.
    composer.json

    package/contact/composer.json

    • After adding autoload psr-4 also add Package Discovery which will help to automatically register its service provider. And this package discovery creates a convenient installation experience for your package’s users.

    • Then Run the composer dump-autoload command, which will help to recognize the packages.

    Step4:

    Next, we need a service provider for our package, so create a src folder and under this folder create ContactServiceProivder.php file and load the routes, views, migrations and configurations package under boot function like this:

    Step5:

    After loading packages, we need to create all files separately under package/contact/src directory as your Form requirements, like this:

    • Create src/routes/web.php file and add code like this:

    • Create Views file: src/views/contact.blade.php file and code like this:

    • Create Controller: src/Http/Controllers/ContactController.php and add code like this:

    Step6:

    Create Model and Migrations:
    Run the command for creating model and migrations: php artisan make:model Contact –m. This command create model and migration in your laravel project so you have to cut model and migration file and paste under your packages and edit and add code like this:
    src/database/migrations/create_contact_table.php

    • Open src/Models/Contact.php file and add code like this:

    Step7:

    Creating Database and Configuration:
    • Create database: open your phpMyadmin and create the database:

    • Next, open .env file and configure database with your laravel project

    • As well as configure your mail driver with your laravel projects: Browse mailtrap.io and login in mailtrap.io, you will get your credentials like this:

    • Then Configure with your .env file, like this:

    • After setting up databases, then run the migrate command: php artisan migrate

    Step8:

    Create Mailable system for sending mail:
    Run php artisan make:mail ContactMailable –markdown=contact.email. This command will create a ContactMailable.php and email.blade.php file in your main Laravel project. You should have to cut these file along with folder and paste it into your package/contact/src directory.
    • Open Mail/ContactMailable.php file and add code like this:

    • Open views/contact/email.blade.php file and add code like this:

    Step9:

    Under your package/contact/src directory create config/contact.php file:
    • Open the config/contact.php file and add code like this:

    • For publishing the package, run the commands: php artisan vendor:publish.

    • After running this command you will get same contact.php file under your main laravel project config folder like this:

    2. Publish in Packagist

    Here with a few steps, I will discuss how you can submit your package in packagist.org, just follow the steps:

    Step1:

    First you need to open packagist.org and create your account using GitHub:
    Packagist.org

    • Sign in

    Step2:

    Next, Upload your package in GitHub directory like this:

    Step3:

    Afer moving your package in GitHub.com, then submit your package in packagist.org. So, copy the link address from GitHub library and paste it packgist.org like this:

    • After submitting the package in packagist.org, you will get your “composer require” command for using this package:

    3. How to use or download this Package:

    After uploading and creating the package, with few steps, I will show you how to download and use this package:

    Step1:

    Download the fresh Laravel project:
    Run the composer command: composer create-project –prefer-dist laravel/laravel TESTPACKG

    Step2:

    Download the package:
    Run composer require command: composer require fincrm/contact. This command will download the contact form in your Laravel project.

    Step3:

    After Package installation, publish config file. Run the command:
    php artisan vendor:publish –provider=”Fincrm\Contact\ContactServiceProvider”
    This command will publish a file contact.php in your config directory.

    Step4:

    You can see the package is installed in your project. Go to your vendor directory and you will see the contact form with package like this:

    Step5:

    Setup Package in your Laravel project:
    • After installing package, create database and configure database with .env file.
    • Browse mailtrap.io and logged in mailtrap.io then configure with it .env file.
    • Next, run the php artisan migrate command.
    • Finally navigate URL: http://127.0.0.1:8000/contact in your browser. Then you will get output like this:

  • Laravel vs WordPress CMS

    Differences between Framework & CMS?

    Framework is a user written, custom code within a predefined set of rules. This allows developers to develop application and modules using library functions and primary programming language. Working with framework developers can build and customized website.
    CMS is stands for Content Management System. CMS gives you the business owner, the ability to manage the content of your website. Working with this you can change text on specific page, add new items, manage inventory, change pricing and perform any number of functions. Once you get into the back-end of your CMS based website, then you do not have to completely rely on your developers to make changes to the website.

    Differences between Laravel & WordPress?

    Laravel is an open source PHP framework. This is based on Model View Controller and developed for the development of web applications. Working with Laravel there must be need of technical knowledge.
    WordPress is open source but as well as it is a Content Management System. It is mainly used for blogs, newspaper and websites. In WordPress, it is not necessary to have a programming knowledge.

    Comparison Laravel Vs WordPress CMS?

    If we compare our Laravel CMS with WordPress CMS, then comparison are like:

     

    Laravel
    CMS

    WordPress
    CMS

    Simplicity

    It
    is complex as compared to WordPress.

    It
    is simpler to Laravel. Simplicity makes it possible for you to get online and
    get publishing, quickly.

    Architecture

    It
    is based on Model View Controller.

    It
    is a Content Management System, and has more features like plugins.

    Framework

    Laravel
    is open source PHP framework.

    Not
    a framework. It is open source and CMS.

    Flexibility

    Using
    Laravel, you can build a data-based website, developed a custom made web
    application with complex features and create a large scale e-commerce
    solution.

    Using
    WordPress, you can create any type of website you want like: a personal blog,
    a photo blog and a business website or website or more.

    Theme System

    In
    Laravel, you have to create your own theme system. It does not provide easily
    theme system like WordPress.

    WordPress
    easily provide theme system for client, other WordPress users or yourself.

    Built-in

    It
    has great built-in capabilities.

    In
    WordPress, functionality extended with help of plugins.

    Language

    In
    Laravel, building a website in different languages is complex task for developers.
    It is not easier like WordPress.

    In
    WordPress building a website in different languages is easier task. WordPress
    provide building a website in 70 or more languages.

  • New Laravel 6.x

    Laravel Releases the new version of Laravel 6 (LTS) in 3rd September 2019. For Laravel 6 (LTS) releases, bug fixes are provided for 2 years and security fixes are provided for 3 years. And for general releases, bug fixes are provided for 6 months and security fixes are provided for 1 year. Here, I will show you the updated table with versions and dates of the latest versions of Laravel:

    Version

    Release

    Bug
    Fixes Until

    Security
    Fixes Until

    5.5
    (LTS)

    August
    30th, 2017

    August
    30th, 2019

    August
    30th, 2020

    5.6

    February
    7th, 2018

    August
    7th, 2018

    February
    7th, 2019

    5.7

    September
    4th, 2018

    March
    4th, 2019

    September
    4th, 2019

    5.8

    February
    26th, 2019

    August
    26th, 2019

    February
    26th, 2020

    6
    (LTS)

    September
    3rd, 2019

    September
    3rd, 2021

    September
    3rd, 2022

    What’s New in Laravel 6?

    In this article, I will discuss the new features in Laravel 6:
    1. Semantic Versioning:
    Semantic versioning is a kind of giving the number Major, Minor and Patch, increment the:

    • Major version when you make incompatible API changes,
    • Minor version when you add functionality in a backwards compatible manner, and
    • Patch version when you make backwards compatible bug fixes.

    So, The Laravel framework (laravel/framework) package follows the versioning standards. This makes the framework consistent with the other first-party Laravel packages which already followed this versioning standard and the Laravel release cycle will remain unchanged.

    2. Ignition Error Page:
    It is a new error page or a new open source exception page for Laravel 6. This is contributed by the team behind Spatie and Beyond code. Ignition is a gorgeous and extensible error page for Laravel application. It is the default error page for all Laravel 6 applications.
    For example: Install Laravel6 and just change the view name in web.php file and run the project. You will get error page like this:
    Ignition offers many benefits over previous release, such as improved Blade error file and line number handling, runnable solutions for common problems, code editing, exception sharing, and an improved UX.
    For example: Install ignition code editor: composer require facade/ignition-code-editor –dev
    And run the project, you will notice code editor in error page, like this:

    3. Improved Authorization Responses:
    In Laravel 6, there is new Gate::inspect method introduced, which make easier the authorization response message. Whereas in previous release of Laravel, it was difficult to retrieve and expose custom authorization messages to end users. The authorization policy’s response and message may be easily retrieved using the Gate::inspect method:

    4. Job Middleware:
    This feature is implemented by Taylor Otwell. Job middleware allow you to wrap custom logic around the execution of queued jobs, reducing boilerplate in the jobs themselves.
    In Laravel 6, Job Middleware allows jobs to run through middleware. The middleware will help you avoid custom logic in the body of your job’s handle() method.

    5. Lazy Collections:
    Laravel 6 introduced a LazyCollection, which is a game-changer for working with an extensive collection of data, including Eloquent model collections. A new Illuminate\Support\LazyCollection class leverages PHP’s generators to allow you to work with very large datasets while keeping memory usage low.
    For example: Imagine you need to iterate through 10,000 Eloquent models. When using traditional Laravel collections, all 10,000 Eloquent models must be loaded into memory at the same time:

    6. Eloquent Subquery:
    This Eloquent Subquery Enhancement is contributed by Jonathan Reinink. Laravel 6 ships with improved support for eloquent subqueries. In this you may now add subqueries by using the new addselect() method, or by passing a closure to the orderBy() and from() method of the query builder.
    For example: Suppose we have a table of flight destinations and a table of flights to destinations. The flights table contain an arrived_at column which indicates when the flight arrived at the destination.
    So, In Laravel 6 using a single query we can select all of the destinations and the name of the flight that most recently arrived at that destination: addSelect().
    In addition, we can use new subquery features added to the query builder orderBy() function to sort all destinations based on when the last flight arrived at that destination. This may be done while executing a single query against the database, like this:

    7. Frontend Scaffolding or Laravel UI:
    In Laravel 6, the frontend scaffolding is now extracted into separate laravel/ui composer package. Means that no Bootstrap or Vue code is present in default framework scaffolding, and the make:auth command has been extracted from the framework as well. When installing a fresh larvel6 application, you will notice that the login and registration scaffolding, as well as the Vue/jquery/bootstrap in your app.js file have been extracted to a new composer package (laravel/ui).
    For example: Install Laravel6 and Laravel5 and open the app.js file of both file. You will get the difference between them:
    • Laravel5.x: open resources/js/app.js file:
    • Laravel6.x: open resources/js/app.js file:
    In Laravel 6, you may install the laravel/ui package and use ui Artisan command to install and restore the traditional Vue/Bootstrap and frontend scaffolding present in previous release of Laravel.
    Installing laravel/ui package: Run composer require laravel/ui –dev
    After installing laravel/ui package then install Authentication scaffolding: Run php artisan ui vue –auth

  • How to make API in Laravel

    In this article, we’ll discuss how to make an API in Laravel. Before building an API, you’ll need to have RESTful endpoints, authentication and JSON responses. So, Laravel offer all of this out of the box. So, in this article, we will go through the steps to make RESTful API in Laravel.
    Before going to the steps, we’ll get some overview about RESTful and Laravel Passport package.

    RESTful action:

    REST is stands for the Representational State Transfer which is an architectural style for network communication between applications and relies on the stateless protocol (Http). REST uses the methods such as GET, PUT, POST and DELETE.

    Laravel Passport:

    For API authentication we’ll use the Laravel Passport package. Laravel already makes it easy to perform authentication via traditional login forms, but in case of APIs, APIs uses the tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides the full OAuth2 server implementation for you Laravel application.

    How to make an API in Laravel?

    In this article we’ll make product reviews API. Users will add, update, delete the products and also view a list of products and review a product.
    Note: Code for this API is available on GitHub: https://github.com/digitalcrm/LaraveAPI.git
    Steps for make an API:

    1. Install Laravel app:

    Run the composer command: “composer create-project –prefer-dist laravel/laravel ProjectName”.

    2. Create Database:

    2.1. Connect Database with your Laravel project. Open database server and create new database
    2.2. Connect with your project, go to “.env” file and change the DB_CONNECTION, Database, User_name and password. In phpMyAdmin the default value of user_name is root and password is null(empty):

    3. Laravel Passport installation:

    3.1. Run the command “composer require laravel/passport”
    3.2. After this composer command, run the migrate command: “php artisan migrate”
    3.3. Now, Install the passport using “php artisan passport:install” command.
    Setup the passport installation:
    After running the installation command, add the “Laravel\Passport\HasApiTokens” to your “App\User model”.
    Next, you should call the “Passport::routes” method within the boot method of your AuthServiceProvider this file is located on “app/Provider/AuthServiceProvider” directory. And don’t forget to add the class of passport.
    Open your config/auth.php configuration file and set the driver option of the api authentication guard to “passport”. This will instruct your application to use Passport’s TokenGuard when authentication incoming API requests.
    How to Get Token in Passport:
    After completing the all steps, now open postman and add few line in Body area such as grant_type, client_id, client_secret, username and password:
    Client_secret will be obtaining from the database “oauth_clients” which is created by the passport automatically after install command.
    For getting username and password, run the auth command: “php artisan make:auth”.
    You will get the authentication in your project. Navigate “localhost:8000/user” in your browser. After this register the user as:
    Next, after all done send the request, you’ll get the token.
    After getting token, copy this token and make it on environment for easily accessing the data in API.

    4. Creating Model:

    4.1. Setup Migration Table:
    Next, create the model for Product and Review. For Product run the command:
    “php artisan make:model Model/Product –a”
    For Review model, run the command: “php artisan make:model Model/Review –a”
    Where “–a” will create the Controller, Model, Factories and Migration files for your application. After running this command, this will create files at your different directories.
    Under “app/” directory, Controller located at “app/Http/Controller” directory and Model located at “app/Model” directory.
    Migration and Factories will be located at under “database/”directory:
    Next after creating models, add some line of code for creating table in database: For Products, open “database/migrations/create_products_table.php” file and add some line of code:
    Open “database/migrations/create_reviews_table.php” file and add line of code for review table:
    Next Run the migrate command; this command will creates the table in database.
    4.2. Use Faker Table:
    Open “database/factories/ProductFactory.php” file and add line of code for creating fake Product table:
    Open “database/factories/ReviewFactory.php” file and add line of code for creating fake Review table:
    After factories, open “database/seeds/DatabaseSeeder.php” file and add line of code for factory data.
    Next, open terminal and enter command “php artisan db:seed”. This command will automatically add fake data into your database table.
    4.3. Define relationship between models:
    Product Model: open “app/Model/Product.php” file and add the relationship between User and Product.
    Review Model: open “app/Model/Review.php” file and define relationship for reviews.

    5. Defining the Controller for API:

    5.1. Open “app/Http/Controller/ProductController.php” file and add the method for fetching the data, store data, update data and delete the data.
    Fetching data add code in index() method:
    Store the data in store() method:
    Update the data in update() method:
    Finally, deleting the data in destroy() method:
    5.2. Open “app/Http/Controller/ReviewController.php” file and add the method for fetching the data, store data, update data and delete the data.
    Fetching data add code in index() method:
    Store the data in store() method:
    Update the data in update() method:
    Finally, deleting the data in destroy() method:

    6. Creating Requests: For Product And Review

    Creating the request for product, enter the command “php artisan make:request ProductRequest”
    Adding code for Product request:
    Creating the request for product, enter the command “php artisan make:request ReviewRequest”
    Adding code for Review request:

    7. Creating Resources:

    Next, we have to define the resources for transforming the model and model collection into JSON. Basically when building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application’s users.
    Command for creating the resources: “php artisan make:resource ResourceName”.
    Creating Resources for Product: in this application we have created the ProductCollection and ProductResource.
    Create ProductResource:
    Create ProductCollection:
    Resource ProductCollection and ProductResource located at “app/Http/Resources/Product” directory.
    Next, open the ProuductResource.php file and add line of code:
    Create ReviewResource: this file located at “app/Http/Resources/ReviewResource.php” directory.
    Next, add line of code under ReviewResource file:

    8. Routes:

    Creating the routes for API, open the “routes/api.php” file and create route for products and reviews.
    Run the command “php artisan route:list” for route the api for products and reviews.

    9. Navigates Routes.

    Open the postman (Postman is a collaboration platform for API development) and run the URL:
    GET Product: http://localhost:8000/api/products:
    PUT Product: http://localhost:8000/api/products/51
    Delete Product: http://localhost:8000/api/products/51
    Review Product: http://localhost:8000/api/products/4

  • Laravel RSS and XML Feed

    In this article, we will discuss about how to create RSS feed in Laravel. But first we will discuss an overview about RSS feed.
    RSS is the type of web feed that allows users and applications to receive regular updates from a website or blog of their choice. RSS stands for Rich Site Summary or it often called Really Simple Syndication. RSS feed is read by an RSS reader or a feed reader, which can be web based, a standalone desktop application or a mobile application.
    An RSS fee is delivered in XML format, allowing maximum compatibility between readers.
    In this article, we are going to use laravelium/feed package for generating RSS feed in Laravel application. So, let’s follow the steps below:

    Step1: Install Laravel Project:

    Open your terminal and run the composer command: “composer create-project –prefer-dist laravel/laravel RssFeed”

    Step2: Install the Packages

    In this step we have to install laravelium/feed package using “composer require laravelium/feed” command.
    After installing the package, we have to add service provider under providers array and aliases array.
    Open “config/app.php” file and add service “Laravelium\Feed\FeedServiceProvider::class,” under providers array like this:
    Next, add services under aliases array like this:
    Publish Configure File:
    Run the command in your terminal for publishing the configure file:
    php artisan vendor:publish –provider=“Laravelium\Feed\FeedServiceProvider”. This process is optional, so, you can skip out this process.

    Step3: Configure SQL Database

    Open your database server and create database like this:
    Next, open .env file and setup database credentials like this:

    Step4: Create Model:

    Next, we have to create model and migration file. So, run the command: php artisan make:model Post –m, here “-m” will create migration file also.
    After running the artisan command, open migration create_posts_table.php file and add line of code like this:
    Next, run the migrate command: php artisan migrate. This command will create tables in your database.

    Step5: Creating Dummy data:

    Now we will create dummy data in your database. Open the database/factories/UserFactory.php file and add the fake table like this:
    Open database/seeds/DatabaseSeeder.php file and add the line like this:
    Next, run database seed command; php artisan db:seed
    After running seed command, 50 records of data feed into post table. You can see database post table like this:

    Step6: Create Controller:

    Now we have to create the controller for rss-feed. Run the command:
    php artisan make:controller PostController
    Next, open the “app/Http/Controller/PostController.php” file and add the following:

    Step7: Define The Routes:

    Open routes/web.php file and define your routes like this:
    Next, open the resources/views/welcome.blade.php file and add an RSS link to the welcome page by adding Feed::link in the section like this:

    Step8: Navigate The Browser:

    Enter the URL: 127.0.0.1:8000/feed and you will get your feed like this: