Category Archives: Laravel

Laravel Tutorials

How to Create Notification in Laravel 8

In this How to Create Notification in Laravel 8 tutorial I will show you how to send notification in laravel 8 application. In this tutorial you will learn to send notification using laravel notification system. Laravel notification system is used to notify users in laravel application. Using laravel notification you can send email, send sms and send slack message notification to user. In this article I will share example to in this example to create notification to send mail in laravel. In laravel you can easily generate a notifications table, bind some information, and send the notification laravel 8. It is easy to generate a notifications table in laravel using artisan command.

How to Create Notification in Laravel 8

In this step by step tutorial I will demonstrate you with example how to create and send notification in laravel using laravel notification system. Please follow the instruction given below:

  1. Install Laravel Project
  2. Set Up Database Connection
  3. Produce Notification Table
  4. Create Notification
  5. Define Route
  6. Create Controller
  7. Summary

Install Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Switch into the project directory using following command.

Set Up Database Connection

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Create Notification Database Table

In this step we will be creating notification table in the database using following artisan command:


Once this command is executed you will find a migration file created under “database/migrations”.

database/migrations/timestamp_create_notifications_table.php

Now, run following command to migrate database schema.

Create Notification in Laravel 8

Now use the following command to create notification.

Above command will generate a new notification file app/Notifications/OffersNotification.php.

Define Notification Route

Define a route to send a notification to a single user, put the following code in routes/web.php to create the route.

routes/web.php

Create Notification Controller

Now, lets create a controller named NotificationController using command given below –

Once the above command executed, it will create a controller file NotificationController.php in app/Http/Controllers/ directory. Open the NotificationController.php file and put the following code in it.

app/Http/Controllers/NotificationController.php

Now we are ready to run our example so lets start the development server using following artisan command –


Now, visit the following link in browser to send the notification –

Laravel 8 Multiple Authentication

In this Laravel 8 Multiple Authentication Tutorial I’ll show you how to create multiple authentication system in laravel 8. In this tutorial you will learn to create or implement multiple authentication in laravel. In this article we will be creating multiple authentication system using custom middleware in laravel 8. Multiple authentication system means to allow multiple users to login in one application according to their roles. In this example we will create a middleware for checking the user’s role whether it is an admin or normal user.

Laravel 8 Multiple Authentication

In this step by step Laravel 8 Multiple Authentication Example we will learn to implement multi auth system using middleware in laravel 8.

  1. Install Laravel Application
  2. Configure Database Connection
  3. Set Up Model and Run Migration
  4. Generate Auth Scaffolding
  5. Set Up Admin Middleware
  6. Set Up Route
  7. Configure Home Controller
  8. Configure Blade View
  9. Configure Login Controller
  10. Seed Database with User Data
  11. Run & Test The Laravel Multi Auth App
  12. The Bottom Line

Download Laravel Application

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Now switch inside the project directory using following command.

Configure Database Connection

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Set Up Model and Run Migration

In this step we will declare the new property ‘is_admin’ in users table using migration. Put the following code inside database/migrations/timestamp_create_users_table.php file.

Now, get inside the app/User.php file and add the newly created is_admin property.

Now, run the migration to create database table using following artisan command:

Generate Auth Scaffolding

In this step we will Install Laravel UI package using following command,


Now, execute the below command on terminal for creating login, registration, forget password and reset password blade files:

Run following command to compile your fresh scaffolding.

Set Up Admin Middleware

In this step we will create middleware for checking the user’s role. It is an admin or normal user.


Open app/Http/middleware/IsAdmin.php and paste the following code.

You have to define the Admin middleware in app/Http/Kernel.php file, so paste the following code inside of $routeMiddleware array.

Set Up Route

In this step, we will create a route for admin and bind it with admin home page. Paste the following code in routes/web.php file.

Configure Home Controller

We have to incorporate the handleAdmin() method inside the HomeController class, open app/Http/Controllers/HomeController.php, and add the following code.

Configure Blade View

Open resources/views/home.blade.php file and put the following code in it.

Now, create and open resources/views/handleAdmin.blade.php file and add the code.

Configure Login Controller

Put the following code in app/Http/Controllers/Auth/LoginController.php file

Seed Database with User Data

Now use the following command to generate a seeder.

Go to database/seeds/DummyUsersSeeder.php file and insert the following data.

Run & Test The Laravel Multi Auth App

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

How to Integrate Paypal Payment Gateway in Laravel 8

In this How to Integrate Paypal Payment Gateway in Laravel 8 tutorial I will show you how to integrate paypal payment gateway in laravel 8 application. In this tutorial you will learn to integrate paypal in laravel application. In this article I will share example to integrate paypal payment gateway in laravel application. We will be using srmklive laravel paypal package for paypal payment gateway integration in laravel 8.

Paypal Payment Gateway

Paypal is one of the most popular payment gateway, that allows us to accept payment from customer. paypal is very simple, hassle free and easy to integrate payment gateway. Integrating paypal payment gateway in laravel is a breeze.

How to Integrate Paypal Payment Gateway in Laravel 8

In this tutorial, you will learn to integrate paypal payment gateway in your laravel project. Follow this step by step tutorial to learn paypal payment gateway integration in laravel.

  1. Install New Laravel Project
  2. Database Settings
  3. Install Essential Composer Package
  4. Inject PayPal Details
  5. Define Imperative Routes
  6. The Controller Archetype
  7. Configure Blade View

Install New Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Database Settings

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Essential Composer Package

In this step, we will install srmklive/paypal Package via the composer dependency manager. Use the following command to install srmklive/paypal Package.


After Installing Image intervention package, we need to add service provider and alias in config/app.php file as following.

config/app.php

config/paypal.php.

Inject PayPal Details

Now we need to put following paypal integration details as required

Define Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Create Controller By Artisan Command

Now, lets create a controller named PayPalPaymentController using command given below –


Once the above command executed, it will create a controller file PayPalPaymentController.php in app/Http/Controllers/ directory. Open the PayPalPaymentController.php file and put the following code in it.

app/Http/Controllers/PayPalPaymentController.php

Configure Blade View

In this step we will create a blade view file to accept paypal payments

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 8 Traits Example Create Use Trait in Laravel

In this Laravel 8 Traits Example Create Use Trait in Laravel tutorial I will show you how to create and use reusable traits in laravel 8 application. In this tutorial you will learn to create and use traits in laravel. In this article I will share example to create and use traits in laravel application.

What Is Laravel Traits?

In this article we are going to learn about Laravel 8 Traits. Traits is a independent reusable class that is that allows you to implement single inheritance in order to reuse it.  With traits we can create a reusable piece of code and inject it in controller and modal in a Laravel application.

Laravel 8 Traits Example: Create & Use Trait in Laravel

In this step by step tutorial I will demonstrate you with example to create and use traits in laravel 8. Please follow the instruction given below:

  1. Traits in Laravel Example
  2. Install New Laravel Application
  3. Configure Database Connection
  4. Model and Migrations
  5. Insert Fake Data
  6. Create a Trait
  7. Using Traits in Laravel

Laravel Traits Example

In this example we will be creating and use a reusable traits.

Install New Laravel Application

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Configure Database Connection

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Model and Migrations

Now, in this step we will create model and migration file. Please run the following command:

Open database/migrations/timestamp_students_table.php file and incorporate the following code.

Next, go to app/Models/Student.php file and add the $fillable property in the Student model as following.

Now, run the migration to create database table using following artisan command:

Insert Fake Data

In this step we will add some test data in students table. Add students table’s value in database/seeds/DatabaseSeeder.php file, it will generate dummy data in database.

Execute following command to generate fake records in the database.

Create Traits in Laravel

Lets create a Traits folder inside app/Http/ directory, then create StudentTrait.php file within the Traits folder. Now, you need to place the following code in app/Http/Traits/StudentTrait.php file:

Using Traits in Laravel

add the following code in routes/web.php file.

Now, lets create a controller named StudentController using command given below –


Insert following code in app/Http/Controllers/StudentController.php file to set the newly created Trait.

Finally, we have to display the data that we conjugated in the Laravel Traits. Insert the following code in resources/views/welcome.blade.php file.

Start Application

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 8 REST API with Passport Authentication Tutorial

In this Laravel 8 REST API with Passport Authentication Tutorial I will show you how to create REST API with passport authentication In laravel. In this tutorial you will learn to create rest api for crud operation with passport authentication In laravel 8 application. In this article I will share example to create rest api with passport authentication in laravel. I will also show you how to install passport auth package in laravel. After installing and configure passport authentication in laravel we will create simple crud operation rest api. In this article we will be creating a post management crud operation rest api with passport authentication in laravel.

Laravel 8 REST API with Passport Authentication Tutorial

In this step by step tutorial I will guide you through create a fully functional restful API with passport authentication in Laravel 8. We will be creating fully functional REST API along with passport Authentication. Please follow the instruction given below:

  1. Install New Laravel Project
  2. Set Up Database
  3. Install Passport Package
  4. Configure Passport Module
  5. Create Post Model & Run Migration
  6. Create a New Controller
  7. Define API Routes
  8. Test Laravel Passport API

Install New Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Set Up Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Passport Package

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

Now, run following command to migrate database schema.

Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.

Configure Passport Module

Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

Next, open app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function, It will evoke the required routes.

Register the PassportServiceProvider class in providers array inside the config/app.php file:

Configure driver for the Passport, get inside the config/auth.php file and make the changes as shown below.

Create Posts Model & Run Migration

Now, in this step we will create model and migration file. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open timestamp_create_posts_table.php migration file and update the function up() method as following:

Next, go to app/Models/Post.php file and add the $fillable property in the Post model as following.

Now, run the migration to create database table using following artisan command:

Create a New Controller

Now, lets create a controller named PassportAuthController using command given below –

Once the above command executed, it will create a controller file PassportAuthController.php in app/Http/Controllers/ directory. Open the PassportAuthController.php file and put the following code in it.

app/Http/Controllers/PassportAuthController.php

Now open app/Models/User.php file and put the following code in it:

Now, lets create a controller named PostController using command given below –


Add the following code in PostController.php file.

Define API Routes

Now we will create rest API auth and crud operation routes. Go to the routes directory and open api.php. Then put the following routes into api.php file:

routes/api.php

Test Laravel 8|7 Passport API

Now we are ready to run our example so lets start the development server using following artisan command –

Now, we will call above create crud and auth apis in postman app:

Register API:

Login Passport API:

Passport Post Create API:


You need to set this access token as a Bearer Token in the Authorization header.

Post List API:

Post Update API:

Post Delete API:

Laravel 8 Dynamic Autocomplete Search with Select2 Example

In this Laravel 8 Dynamic Autocomplete Search with Select2 Example tutorial, I will show you how to create dynamic ajax driven autocomplete search with select2 in laravel 8 application. In this tutorial you will learn to create dynamic autocomplete search with select2 in laravel 8. In this article I will share example to create dynamic autocomplete search with select2 in laravel 8. In this example we will be using Select2 jQuery plugin in Laravel 8 with AJAX. The jquery Select2 plugin is used to create dynamic autocomplete input with several options.

Laravel 8 Dynamic Autocomplete Search with Select2 Example

In this step by step tutorial I will demonstrate you how to implement Select2 dynamic autocomplete search in laravel application. Please follow the instruction given below:

  1. Install Laravel Application
  2. Configure Database
  3. Model & Migration
  4. Create Routes
  5. Create Autocomplete Controller
  6. Create View
  7. Test App

Install Laravel Application

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Switch into the project directory using following command.

Configure Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Now we will add some fake data to be searched. Go to PHPMyAdmin and run the following SQL query.

Model & Migration

Now, in this step we will create model and migration file. Please run the following command:

In database/migrations/timestamp_create_movies_table file add the table values that you want to generate in database.

Create the app/Models/Movie.php and add the given values within the $fillable array.

Now, run following command to migrate database schema.

Create Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Create Autocomplete Search Controller

Now, lets create a controller named Select2SearchController using command given below –

Once the above command executed, it will create a controller file Select2SearchController.php in app/Http/Controllers/ directory. Open the Select2SearchController.php file and put the following code in it.

app/Http/Controllers/Select2SearchController.php

Create View

In this step we will create resources/views/home.blade.php file, then put the following code.

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

How to Enable CORS in Laravel

In this How to Enable CORS in Laravel tutorial, I will show you how to enable CORS (Cross-Origin Resource Sharing) in Laravel 8 application. In this tutorial you will learn to enable CORS in laravel application. You will also learn to install and configure CORS to get rid of CORS header ‘access-control-allow-origin’ missing problem. When a request is made between two between two different servers or domains because of security concern we usually get No ‘Access-Control-Allow-Origin’ warning. Enabling CORS authenticate the resource sharing between two different domains.

How to Enable CORS in Laravel

In this step by step tutorial I will demonstrate you how to enable CORS (Cross-Origin Resource Sharing) in Laravel 8. Please follow the instruction given below:

  1. Laravel CORS Example
  2. CORS Middleware Nitty-Gritty
  3. Create API in Laravel
  4. Make Http GET Request with AJAX
  5. Malevolent Laravel CORS Error
  6. Installing CORS Package in Laravel
  7. Registering CORS Middleware

Laravel 8 CORS Example

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

switch into the project directory using following command:

Run the command to start the laravel app.

CORS Middleware Nitty-Gritty

After installing laravel application there would be a config/cors.php file generated. Below is list of cors related configurations available in file.

  • CORS configuration paths
  • Allowed methods
  • Allowed origins patterns
  • Allowed headers
  • Exposed headers
  • Max age
  • Supports credentials

config/cors.php

Create API in Laravel

Go to routes/api.php and put the following code to enable CORS in API.

Make Http GET Request with AJAX

In this step we will create a demo.html file and make Http GET request using AJAX to laravel api.

Making ajax request we will get CORS related error (No ‘Access-Control-Allow-Origin’ header is present on the requested resource.) because we have two different domain trying to exchange data.

Installing CORS Package in Laravel

In this step, we will install fruitcake/laravel-cors Package via the composer dependency manager. Use the following command to install fruitcake/laravel-cors Package.

Registering CORS Middleware

Now we have to include \Fruitcake\Cors\HandleCors class at the top inside $middleware array to enable CORS for all our routes in app/Http/Kernel.php file.

This way we have enabled CORS in our laravel application.

Laravel 8 WhereNotIn Database Query Examples

In this Laravel 8 WhereNotIn Database Query Examples tutorial I will show you how to use where not in query in laravel. In this tutorial you will learn to use WhereNotIn with laravel query builder and eloquent model. In this article I will share various example to use WhereNotIn  query in laravel. In this tutorial you will learn about syntax and use of laravel WhereNotIn method.

Laravel WhereNotIn Database Query

Laravel WhereNotIn method will return the records with not having column values in provided list. Here is the general syntax of the laravel WhereNotIn method:

Syntax:-

In wherenotin() you need to pass two argument one is column name and another if array of ids or anything that you want.

Laravel WhereNotIn with Query Builder

In this example we will use laravel WhereNotIn method with query builder.

Example:-

Laravel WhereNotIn with Laravel Model

In this example we will use laravel WhereNotIn method with laravel model.

Example:-

How to Use WhereIn Query in Laravel

In this Laravel 8 WhereIn Database Query Examples tutorial I will show you how to use WhereIn query in laravel. In this tutorial you will learn to use WhereIn with laravel query builder and eloquent model. In this article I will share various example to use WhereIn query in laravel. In this tutorial you will learn about syntax and use of laravel WhereIn method.

Laravel WhereIn Database Query

Laravel WhereIn method will return the records with having column values in provided list. Here is the general syntax of the laravel WhereIn method:

Syntax:-

Laravel WhereIn Query with Laravel Query Builder

In this example we will use laravel WhereIn method with query builder.

Example:-

Laravel WhereIn Query with Laravel Model

In this example we will use laravel WhereIn method with laravel model.

Example:-

Simple way to Print or Get Last Executed Query in Laravel 8

In this Simple way to Print or Get Last Executed Query in Laravel 8 tutorial I will show you how to print last executed query in laravel application. In this tutorial you will learn to get or print last executed query. In this article I will share various example to print last executed query. I will also show you simple way to get last executed query in laravel. You will also learn to output of print sql query.

Simple way to Print or Get Last Executed Query in Laravel 8

In this step by step tutorial I will demonstrates to get sql query in laravel 8 using toSql(), DB::enableQueryLog() and DB::getQueryLog().

Example 1:-

Output:-

Example 2:-

Output:-

Example 3:-

Output:-

Laravel 8 Eloquent WHERE Like Query Example Tutorial

In this Laravel 8 Eloquent WHERE Like Query Example Tutorial I’ll show you how to use laravel like with where method with query builder in laravel. You also learn how to use like in where eloquent method in search query. I’ll also show you how to use laravel where like for multiple like condition. In this tutorial I’ll demonstrate you different use case of the laravel whereLike method with examples.

Laravel 8 Eloquent WHERE Like Query Example Tutorial

Laravel whereLike() method is simple implementation of MySQL Like keyword. In laravel you can use % wildcard character with whereLike condition in similar as you do in mysql like.

Like Query with WHERE Condition in Laravel with Query Builder

In this example I have demonstrated you how to use “LIKE” in where method with query builder.

Like Query with Laravel Model

In this example I have demonstrated you how to use “LIKE” in where method with laravel model.

Laravel 8 Eloquent Multiple Where Clause Query Example

In this Laravel 8 Eloquent Multiple Where Clause Query Example tutorial, I’ll show you how to use multiple where clause in laravel eloquent query. In this tutorial you will learn to use or apply multiple where clause in laravel query. In this article I will share various example to use multiple where clause with laravel query builder and eloquent model. When you fetch data from database tables you come to situation when you need to pass multiple conditions in laravel query. In laravel eloquent queries this can be done by providing multiple where clause in laravel eloquent query.

Laravel 8 Eloquent Multiple Where Clause Query Example

Below is general syntax of where() condition.

Multiple Where Condition with Query Builder

In this example we will be using Multiple where conditions in laravel query builder:

Laravel Multiple Where Clause with Eloquent Model

In this example we will be using Multiple where conditions in laravel model: