How to reusing a Controller Method for Multiple Actions in Laravel

How to reusing a Controller Method for Multiple Actions in Laravel

this is Adi with another Laravel tutorial. This time, I wanted to cover my basic solution to a problem we might all have when building web apps using only blade templates.

Let’s take this example scenario and explore possible solutions. We have an index method on, let’s say UsersController, which lists all the users from our database. Each user has a Subscription and we want to change their subscription. How would you go about doing this with pure blade templates?

There are multiple solutions to this, one would be to show a different page where we can select a plan from a dropdown, and it gets submitted. Another solution would be to have a User’s details page, where we can change the subscription and that gets submitted.

But I want to show you how I do it with this particular method. I open a modal in the index page where all users are listed, we choose the subscription, and the form gets submitted. This all happens from the same page. This is just one example, but if you use this method well, you would be able to add, edit, confirm, show other messages all on the same page, kind of like an SPA, but each page gets loaded from the server.

You can find the source code here.

Getting Started

First, let me explain how my solution works. This solution works by changing the URL parameters of the index method of the UsersController in our example case.

  • /users — This page lists all users

  • /users?edit=12 — The same index page will display a model to edit the details of the user with id 12.

  • /users?delete=12 — This displays a confirmation modal on the same index page.

As you can see, this solution can be expanded to do many more actions. In our controller, we identify which action is being requested using the URL parameter; we then send the response accordingly. Also, in the view, we display a model if a certain variable is sent to the view.

Let’s see some code, so you can understand this better.

Controller

This is image title

As you see, we have an empty variable $edit. This will be populated if there’s an edit parameter in the request URL. This variable will be sent to view whether it has value or not.

Simple Logic, ah.

View

This is image title

Now, in the view, we include the model template only if the $edit is set and not when it’s empty. Also, when you loop through the users to show them in a table or somewhere, use this helper route('users.index', ['edit' => $edit->id]) to add ?edit= to the request URL.

This is image title

Now, for the final part of the view, we need the markup for our model and the JavaScript logic to show it when the page is loaded. The above markup is a pretty basic Bootstrap modal and a way to show it when the page is loaded.

Conclusion

I hope this trick helps you in some way. You can extend this tutorial to add as many actions as you need. I am aware that this is a rudimentary way to implement such features. A better more stable way to implement them would be to build a Single Page Applications (SPA).

If you see any comments or questions, let me know in the comments below!

laravel php

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

10 Laravel Quick Tips

Spread the loveTip 1. Controllers Having Single Action In some situations you need a single action in a controller, if this is the case in Laravel you can achieve it by __invoke() method. Routes: Artisan command to generate this controller:…Read More→

How to Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel is the most popular, free, and open-source PHP framework in the world, known for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some of the best web development tools required for large, robust, and modern applications. In this article, you will learn how to install the Laravel PHP Framework on Ubuntu 20.04 server running on the Nginx web server.