Category Archives: Blog

Blog

Laravel Separate Admin Panel | Multiple Authentication System Using Guards

Laravel 5.8 Multiple Authentication Using Custom Guard

In this Laravel multi (auth) authentication tutorial we will learn how to create separate admin panel or login using custom guard. Laravel multi (auth) authentication system allows to create multiple users login in single application.

While developing a laravel application there comes situations where we want create separate user login or separate user/admin panel for users having different privileges or access rights in same application. In laravel this can be achieved in following ways –

1. Laravel multi (auth) authentication using middleware

2. Laravel multi (auth) authentication Using Guards

In one of my previous article we have created separate admin login using laravel multi (auth) authentication using MIddleware In this article we will creating separate admin login using laravel multi (auth) authentication system with guard.

Install Laravel 5.8

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

Configure Database In .env file

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 User Authentication Scaffolding

Laravel comes with in-built user authentication system, use the below command to generate default authentication scaffolding for user –

This command will generate required authentication Controller files, views and add routes in our web.php routes file that are required for user authentication system.

Now, we have to define table schema for admins table. Open terminal and use the following artisan command to generate <timestamp>create_admins_table.php migration.

Once this command is executed you will find a migration file created under “database/migrations”. Open migration file created (<timestamp>create_admins_table.php) and modify up method as following –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the admins table will be created in database along with migrations, password_resets and users table.

Next we need to create a Admin model class. To make the model for the admins, run the following command:

Again our Admin model is really simple for now, this model will be like the user model and extends the Authenticable class. Open Admin model in app/Admin.php and add the following:

app/Admin.php

Next we need to add a new guard for admin. Laravel guards are used for authentication which manages multiple authenticated instances from multiple tables. Open config/auth.php file and add a custom guard and provider for admins. Add admin guard in guards array as following:

Now we need to add a provider for admins in providers array as following:

Next, we need to create the route for admin authentication. Open the routes/web.php file and add the following code in it.

Now, we will create admin login and home/dashboard controller files. For simplicity we’ll copy user auth login controller file:

into app/Http/Controllers/Admin/Auth and fix the class namespaces and update Admin LoginController as following:

Create HomeController.php file into app/Http/Controllers/Admin directory and update it as following:

Now we need to update the guest middleware RedirectIfAuthenticated for the admin auth guard. Open app/Http/Middleware/RedirectIfAuthenticated.php and add a check for the admin guard, so the handle method will now look something like this:

Update Exception Handler

At the moment if an user is authenticated they will redirect back to the same place, no matter what the guard is. Open app/Exceptions/Handler.php and add the unauthenticated method as following:

app/Exceptions/Handler.php

Add following directives on top:

Add following method in handler class:

This will override the parent handler method.

Next we we will create all relevant views files for admin.

Admin Layout:-

Create the following directory resources/views/admin/layouts and in it we’ll add a new file app.blade.php and put following code in it:

resources/views/admin/layouts/app.blade.php

Admin Login:-

Create the following directory resources/views/admin/auth and in it we’ll add a new file login.blade.php and put following code in it:

resources/views/admin/auth/login.blade.php

Admin Home/Dashboard:-

Create the following directory resources/views/admin/ and in it we’ll add a new file home.blade.php and put following code in it:

resources/views/admin/home.blade.php

Create Admin Seeder

Generate a admin seeder file with following artisan command:

Lets open database/seeds/AdminsTableSeeder.php file created and populate admin user in it as following:

Call Admin Seeder

Lets open database/seeds/DatabaseSeeder.php and call AdminsTableSeeder in run method as following:

database/seeds/DatabaseSeeder.php

Run Admin Seeder

Lets open terminal and run seeder using following artisan command:

Start Application 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 –

http://localhost:8000/admin/login

Output:-

admin_login

http://localhost:8000/admin/

Output:-

admin_home

http://localhost:8000/register/

Output:-

user_register

http://localhost:8000/login/

Output:-

user_login

http://localhost:8000/home/

Output:-

user_dashboard

 

Laravel Fix 150 “Foreign key constraint is incorrectly formed” error In Migration

Laravel “Foreign key constraint is incorrectly formed” Error

In Laravel 5.8, sometime when you generate a create table migration with a foreign key constraints, running migration may encounter 150 “Foreign key constraint is incorrectly formed” error. This is mainly happens when you take foreign key column of integer type instead of bigInteger, and this new laravel convention was causing this error. In Laravel 5.8, when you create a new table migration it will be generated with an ‘id’ column of bigInteger type instead of integer like old laravel version.

Instead of (in Laravel Old versions):

In that case we have to use bigInteger for foreign key column instead of an integer. So your code will look like this:
Example:-

Or you could also use increments instead of bigIncrements for ‘id’ column in table creation of reference table.

instead of:

The main difference between Integer and BigInteger is of their size:
int => 32-bit
bigint => 64-bit

Laravel Clear View Cache

Laravel Clear View Cache

In this Laravel Clear View Cache tutorial, I’ll show you how to clear view cache in laravel application. You’ll learn to clear view cache using artisan command as well as learn to clear laravel view cache on shared host from browser.

Clear View Cache Using Artisan Command

To clear compiled view files cache in laravel use the php artisan view:clear artisan command as given below :

Clear View Cache On Shared Host

Most of the times in shared hosting servers we don’t have SSH access to the server. In that case, to clear laravel view cache we have define a route in our application’s routes/web.php file that invoke the laravel clear view cache command. This way we can clear view cache by accessing specific routes in the browser.

Laravel Clear config cache

Laravel Clear Config Cache

In this Laravel Clear config Cache tutorial, I’ll show you how to clear config cache in laravel application. You’ll learn to clear config cache using artisan command as well as learn to clear laravel config cache on shared host from browser.

Clear Config Cache Using Artisan Command

To clear config cache in laravel use the php artisan config:cache artisan command as given below :

Laravel Clear Config Cache On Shared Host

Most of the times in shared hosting servers we don’t have SSH access to the server. In that case, to clear laravel config cache we have define a route in our application’s routes/web.php file that invoke the laravel clear config cache command. This way we can clear config cache by accessing specific routes in the browser.

Laravel Clear route cache

Laravel Clear Route Cache

In this Laravel Clear Route Cache tutorial, I’ll show you how to clear route cache in laravel application. You’ll learn to clear route cache using artisan command as well as learn to clear laravel route cache on shared host from browser.

Clear Route Cache Using Artisan Command

To clear route cache in laravel use the php artisan route:clear artisan command as given below :

Laravel Clear Route Cache On Shared Host

Most of the times in shared hosting servers we don’t have SSH access to the server. In that case, to clear laravel route cache we have define a route in our application’s routes/web.php file that invoke the laravel clear route cache command. This way we can clear route cache by accessing specific routes in the browser.

Laravel Clear App Cache

Laravel Clear App Cache

In this Laravel Clear App Cache tutorial, I’ll show you how to clear app cache in laravel application. You’ll learn to clear app cache using artisan command as well as learn to clear laravel app cache on shared host from browser.

Clear App Cache Using Artisan Command

To clear laravel application cache use the php artisan cache:clear artisan command as given below :

Clear App Cache On Shared Host

Most of the times in shared hosting servers we don’t have SSH access to the server. In that case, to clear laravel application cache we have define a route in our application’s routes/web.php file that invoke the laravel clear application cache command. This way we can clear application cache by accessing specific routes in the browser.

Laravel Clear Cache Using Artisan Command

Larave Clear Cache Using Artisan Command

In this laravel clear cache tutorial, I’ll show you various artisan commands to clear laravel cache of different types. You will also learn to clear cache on share hosting.

In Laravel, while developing a laravel application it is very often that changes we made are not reflecting. This is usually happens because of the laravel cache. Laravel provides various caching systems for developing fast loading laravel applications.

In this laravel clear cache example, we will learn to clear cache from command line (CLI), we will see how to clear cache from blade (views), routes,config etc using the command line and artisan command.

Useful Laravel Clear Cache commands

Here, we will discuss about various artisans commands used to clear different laravel cache in laravel applications.

Laravel Clear App Cache

To clear laravel application cache use the php artisan cache:clear artisan command as given below :

Laravel Clear route cache

To clear route cache in laravel use the php artisan route:clear artisan command as given below :

Laravel Clear config cache

To clear config cache in laravel use the php artisan config:cache artisan command as given below :

Laravel Clear View Cache

To clear compiled view files cache in laravel use the php artisan view:clear artisan command as given below :

Laravel Reoptimized Class

Use the following laravel command to Reoptimized Class In laravel :

Laravel Clear Cache On Shared Host

Most of the times in shared hosting servers we don’t have SSH access to the server. In that case, to clear laravel cache we have define routes in our application’s routes/web.php file that invoke the various laravel clear cache commands. This way we can clear Laravel cache by accessing specific routes in the browser.

Laravel Clear Route Cache from Browser

Laravel Clear Config Cache from Browser

Laravel Clear Application Cache from Browser

Laravel Clear View Cache from Browser

Laravel Simple Qr Code Generator Example

In this article, I’ll show you how to generate qr codes with any text, size, color, background color, format like png, eps, svg. In this laravel qr code example, we will be using simple-qrcode package to generate QR codes.The simple-qrcode package is used to generate qr code in your laravel application. The simple-qrcode package has many options for creating qr codes. You can also create qr code for geo, phone number, text message using simple qrcode package. The simple-qrcode package is very easy to install and use.

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have not installed it yet you can follow laravel 5 installation instruction here.

Install Laravel 5.8

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

Install simple-qrcode Package

In this step, we will install simple-qrcode Package via the composer dependency manager. Use the following command to install simple-qrcode Package.

Register Package

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

config/app.php

Basic Usage Of simple-qrcode Package

Qr code with specified string can be generated as follows –

Syntax:-

Simple QR Code :-

Simple Qr code with specified string can be generated using generate() method. Add a simple-qr-code route to your routes/web.php file to return simple QR code as follows –

Here, size() function is used to specify the size of the QR Code. Visit (http://localhost:8000/simple-qr-code) to see the QR code. Scan the QR code and you will see the text. You can also display the QR code in Blade file as follows –

QR Code with Color :-

You can also set color for QR code pattern and the background using color(R, G, B) and backgroundColor(R, G, B) methods.

Add a color-qr-code route to your routes/web.php file to return colored QR code as follows –

Visit (http://localhost:8000/color-qr-code) to see the colored QR code. Or you can simply display the colored QR code in Blade file as follows –

QR Code with Image :-

You can also place an image in QR code using the merge(“file_name.png”) method, this method only accept png files and you also need to format the response back to png.

Add a qr-code-with-image route to your routes/web.php file to return QR code with image as follows –

Visit (http://localhost:8000/qr-code-with-image) to see the QR code with Image merged. Or you can simply display the QR code with Image in Blade file using <img> tag as follows –

Email QR code :-

You can encode QR codes that automatically fill-in email address, subject and body of the email.

Syntax:-

Example:-

QR Code Phone Number :-

You can encode QR codes such that it automatically open up the phone dialer and dial the phone number.

Syntax:-

Example:-

QR Code Text Message :-

You can encode QR codes such that it automatically open up the phone messenger and fill-in phone number or the message body.

Syntax:-

Example:-

QR Code Geo co-ordinates :-

You can encode QR codes such that it automatically open up map application with the specified location.

Syntax:-

Example:-

Create Blade / View Files

In this step, we will create view/blade file to generate and display QR Codes. Lets create a blade file “index.blade.php” in “resources/views/qrcode/” directory and put the following code in it respectively.

resources/views/qrcode/index.blade.php

Create Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/generate-qr-code

Output:-

laravel-5-8-generate-qr-code-tutorial-1

http://localhost:8000/simple-qr-code

Output:-

laravel-5-8-generate-simple-qr-code

http://localhost:8000/color-qr-code

Output:-

laravel-5-8-generate-color-qr-code

http://localhost:8000/qr-code-with-image

Output:-

laravel-5-8-generate-qr-code-with-image

How to Get User IP Address in PHP

How to Get User IP Address in PHP

There may be many occasions we need to get user IP address to track user’s activity. In PHP, you can simply get the visitor’s IP address using the $_SERVER[‘REMOTE_ADDR’] variable.

  • $_SERVER[‘REMOTE_ADDR’] – It Returns the IP address of the current user.

Example:-

But, $_SERVER[‘REMOTE_ADDR’] may not always return the true IP address of the client. If user is connected to the Internet through Proxy Server then $_SERVER[‘REMOTE_ADDR’] in PHP just returns the the IP address of the proxy server not of the client’s machine. So here, I have create a simple function in PHP to find the real IP address of the visitor’s machine.

Simple PHP function to get client IP address

Laravel Custom Datatables filter and Search

In this tutorial, I’ll show you how to add custom search or data filter to datatables. In this tutorial, we will be using yajra datatable package for listing of records with pagination, sorting and filter (search) feature. Laravel Yajra datatables package comes with many built-in features for searching and sorting functionality. In this article you will learn to add custom field for searching data without reloading or refreshing of page.

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have not installed it yet you can follow laravel 5 installation instruction here.

Install Laravel 5.8

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

Configure Database In .env file

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.

.env

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Create Database Table

Now, we have to define table schema for posts table. Open terminal and let’s run the following command to generate a migration file to create posts table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Post using below command.

Once, the above command is executed it will create a model file Post.php in app directory. Next, we have to assign fillable fields using fillable property inside Post.php file. Open app/Post.php file and put the following code in it –

app/Post.php

Install Yajra Datatable Package

In this step, we will install Yajra Datatables Package via the composer dependency manager. Use the following command to install Yajra Datatables Package.

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

config/app.php

Create Controller

Next, we have to create a controller to handle Ajax CRUD Operations. Create a controller named AjaxCrudController using command given below –

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

app/Http/Controllers/dtable/AjaxCrudController.php

Create Blade / View Files

In this step, we will create view/blade file to perform CRUD Operations. Lets create a blade file “custom_filter.blade.php” in “resources/views/dtable/” directory and put the following code in it respectively.

resources/views/dtable/custom_filter.blade.php

Create Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/dtable-posts-lists

Output 1:-

laravel-5-8-custom-datatables-filter-and-search-1

Output 2:-

Select “Start Date” and “End Date” and hit Submit button to display corresponding posts.

laravel-5-8-custom-datatables-filter-and-search-2

How to Get Site URL In Laravel

How to Get Site URL In Laravel

In this article, I’ll show you how to get site url in your laravel controller, blade file, model etc. Many times in laravel application development we need to get base url of our laravel application. There follwoing ways we can get site url in our laravel 5 application. In laravel 5 we can get site url or base url using url helper and url facade.

Get Site url Using URL Helper

Syntax:-

Example :-

or

Get Site url Using URL Facade

Syntax:-


Example:-

Laravel 5.8 Razorpay Payment Gateway Integration

Laravel 5.8 Razorpay Payment Gateway Integration

In this tutorial, you will learn to integrate Razorpay payment gateway in your laravel 5.8 project. Before starting with this tutorial you would require a Razorpay developer account, from there you will get the api key and secret key require for the integration. After the successful integration you will be able to collect payment via simple payment form which allow customer to provide card information like card number, expiry date and a CVC code. Follow this step by step tutorial to learn Razorpay payment gateway integration in laravel 5.8.

Install Laravel 5.8

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

Configure Database In .env file

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.

.env

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Create Model and Migration

Now, we require to create a Payment tables to keep the payment records. so let’s run the following command to generate Payment model along with a migration file to create Payment table in our database.

Once the above command is executed a migration file Payments.php file is created, open the database/migrations/Payments.php file and put the following code in it.

Now, use the command to run migration –

Create Routes

After this, we need to add following routes in “routes/web.php” to display product listing, to store payment form and to payment acknowledgement. Lets open “routes/web.php” file and add following route.

routes/web.php

Here, first route is for product listing, second is to store payments_id and users information provided by razorpay payment gateway.

Create Controller

Next, we have to create a controller to handle payments. Create a controller named RazorpayController using command given below –

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

app/Http/Controllers/razorpay/PaymentController.php

In this controller, we have following methods –

show_products() :- It lists Store Items

pay_success() :- To handle payment

thank_you() :- Payment Acknowledgement

Create Blade / View Files

In this step, we will create view/blade file to accept razorpay payment. Lets create two blade files “payment.blade.php” and “thankyou.blade.php” in “resources/views/razorpay/” directory and put the following code in it respectively.

resources/views/razorpay/payment.blade.php

You must replace secret key that you get from razorpay payment gateway here –

resources/views/razorpay/thankyou.blade.php

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 –

http://localhost:8000/my-store

Output:-

laravel-5-8-razorpay-payment-gateway-integration-1

Click “Order Now” for any product of your choice

laravel-5-8-razorpay-payment-gateway-integration-2

laravel-5-8-razorpay-payment-gateway-integration-3

Use the following test card details to test the payment.

Test Credit Card Info :-

Card No :- 4242424242424242 / 4012888888881881
Month :- Any future month
Year :- Any future Year
CVV :- Any 3 digit Number