DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • MLOps: Definition, Importance, and Implementation
  • System Testing and Best Practices
  • Hibernate Get vs. Load
  • How to Handle Secrets in Kubernetes
  1. DZone
  2. Coding
  3. Frameworks
  4. Reusing a Controller Method for Multiple Actions in Laravel

Reusing a Controller Method for Multiple Actions in Laravel

Make your PHP more efficient with this tutorial.

Adi Sk user avatar by
Adi Sk
·
Nov. 08, 19 · Tutorial
Like (4)
Save
Tweet
Share
9.75K Views

Join the DZone community and get the full member experience.

Join For Free

mission-control

Hi all, 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.

You may also like: The Eight Biggest Laravel Development Mistakes You Can Easily Avoid.

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

Controller Logic

Controller logic

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

View Logic

View logic

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.

Modal logic

Modal logic

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!


Further Reading

  • A Five Minute Solution for Viewing Logs in a Laravel Application.
  • Create Your First Laravel App and Add Authentication.
  • The Ultimate Vue.js and Laravel CRUD Tutorial.
Laravel

Published at DZone with permission of Adi Sk. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • MLOps: Definition, Importance, and Implementation
  • System Testing and Best Practices
  • Hibernate Get vs. Load
  • How to Handle Secrets in Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: