Category Archives: Laravel

Laravel Tutorials

Laravel Passing Multiple Parameters In Route to Controller

Today in this tutorial, we will learn to pass multiple parameters through named route to controller method. In this Laravel Passing Multiple Parameters In Route to Controller tutorial you will learn to pass multiple parameters using route to controller. You will also learn to define route with multiple parameters in laravel so that can access them in controller method.

Passing Multiple Parameters Using Route to Controller

Sometimes we need to pass multiple parameters in URL so that we can get those parameters in controller method to perform required action. In this example, will first define a route with multiple parameters and then we will add a controller method accepting multiple parameters. Then we will setup a link with named route having multiple parameters.

Define route with multiple parameters

Now we will learn to define route with multiple parameters.

Syntax (route):-

Below is basic syntax to define a get request route accepting multiple parameters:

Example:-

Now, lets define a example get route accepting multiple parameters as below:

Define Controller Method With Multiple Parameters

Now we will learn to define a controller method accepting multiple parameters.

Syntax (Controller Method):-

Below is basic syntax for controller method accepting multiple parameters as below:

Example:-

Define Named Route With Multiple Parameters

Now, we can setup a named route link in view as below:

Laravel Session Not Working In Constructor

Laravel Session Not Working In Constructor

Back in laravel 5.2, we were able to interact with the session directly in a controller constructor. However, this has changed in laravel 5.3. Now in Laravel 5.3, you can’t access the session or authenticated user in your controller’s constructor because the middleware has not run yet. As an alternative, you may define a Closure based middleware directly in your controller’s constructor. Using an inline middleware, we can directly hook into the request’s routing, so that we have full access to the session:

Laravel Inline middleware

Using an inline middleware, we can directly hook into the request’s routing, so that we have full access to the session:

Example:-

Laravel Prevent Browser Back Button After Logout

Laravel Prevent Browser Back Button After Logout

In this example, we will learn to prevent browser back button after user logout, so that he will not be able to access auth protected routes or pages after logout.

In Laravel, it is quite a common problem that when user logout and then click on the back button in browser the browser shows the last loaded page from the website. In this article, I’ll show you how to prevent back button after logout in Laravel application. In Laravel, when a user logout from application and then hit the browser back button then he will be sent back to the page he was visiting before logout. Ideally, user should not be allowed to access auth middleware protected routes or pages. User should be redirect back to application login page instead of the page he was visiting.

In this tutorial, we will prevent this issue by using laravel middleware. We will create a middleware and use it with routes to prevent back button history.

Create New Middleware

In this step, we will create a new middleware using following command:

Middleware Configuration

Open up PreventBackHistory.php file in app/Http/Middleware folder and replace codes with the following codes below:

app/Http/Middleware/PreventBackHistory.php

Register Middleware

In this step we have to register middleware, open Kernel.php in app/Http folder and add a new middleware in $routeMiddleware variable array as below:

app/Http/Kernel.php

Add Middleware in Route

Now open routes/web.php files and add “prevent-back-history” middleware in route file as below:

routes/web.php

Laravel Clear Cache After Logout

Laravel Clear Cache After Logout

In this example, we will learn to destroy session, cookies, cache and prevent browser back button after user logout, so that he will not be able to access auth protected routes or pages after logout.

In Laravel, it is quite a common problem that when user logout and then click on the back button in browser the browser shows the last loaded page from the website. In this article, I’ll show you how to prevent back button after logout in Laravel application. In Laravel, when a user logout from application and then hit the browser back button then he will be sent back to the page he was visiting before logout. Ideally, user should not be allowed to access auth middleware protected routes or pages. User should be redirect back to application login page instead of the page he was visiting.

Here is the logout function code that worked for me:

Here, we are flushing the session and cache then after redirecting user to login page.

Laravel Clear Cache on Shared Hosting without Artisan command

Laravel Clear Cache on Shared Hosting

In this laravel clear cache shared hosting tutorial, I’ll show you how to clear cache on shared hosting using laravel route. In shared host we dont have access to command line (CLI), in this tutorial I’ll show you to clear cache without artisan command.To clear laravel cache we have to 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 On Shared Hosting

Lets define “route-cache” route in routes/web.php file to clear route cache without using artisan command.

Laravel Clear Config Cache On Shared Hosting

Lets define “config-cache” route in routes/web.php file to clear config cache without using artisan command.

Laravel Clear Application Cache On Shared Hosting

Lets define “clear-cache” route in routes/web.php file to clear application cache without using artisan command.

Laravel Clear View Cache On Shared Hosting

Lets define “view-clear” route in routes/web.php file to clear view cache without using artisan command.

Insert data using Database Seeder in Laravel

Insert data using Database Seeder in Laravel

There are many instances where we need a populated database with test data in order to test the working of various operation. Populating Test data manually is time consuming, but Laravel provides a simple method of seeding test data in your database automatically instead doing it manually. In laravel you are required to created seeder classed per table, all these classes are stored in the database/seeds directory.

Today in this article, we will learn how to insert data using Database Seeder in Laravel. I’ll show you how to create database seeder class for a table, seed data in seeder, call database seeder and run database seeder. In this example, we assume that we already have admins table created and wants to seed some test data into it.

Create Seeder Using Artisan Command

Seeder class file can be generated using following artisan command:

Syntax:-

Example:-

Once the above command is executed, this will generate a seeder class file in the database/seeds directory. This class has a default run() method.

database/seeds/AdminsTableSeeder.php

Add Data In Seeder Class file

Lets open seeder class file and within default run() method, we can populate single or multiple records as following:

Add Single Record:-

Add Multiple Records:-

Call Seeder

Laravel comes with a default seeder class named DatabaseSeeder which is executed by default when the db:seed command invoked. This class can be used to call user-defined seeders like this:

Run Seeder

Once the seeder classes are setup, we are ready to run laravel seeders using following artisan command:

We can also run a specific seeder class directly without adding it to the DatabaseSeeder class as following:

Call Seeder from Route

Call Seeder from Controller

Call Seeder from Migration

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.