Sharing variables between controllers in LaravelPassing data between controllers in Laravel

Recently, I started learning Laravel and came across a scenario where I have two controllers: CartController with the function finalizeCart and PaymentController with the function postPayment. Now, I am trying to figure out how to transfer the variable "TotalPrice" from CartController to PaymentController. Does anyone know how to pass values directly from one controller to another?

Answer №1

To achieve this, start by implementing the following code in your initial controller:

Session::put('key', 'value');

Next, retrieve the value in a separate controller using:

Session::get('key');

For further guidance, refer to the documentation available at http://laravel.com/docs/5.0/session#session-usage

Answer №2

One alternative method for transferring data between controllers is using cookies

  • In your initial controller:

    $response = Response::make('Hello World');
    
    return $response->withCookie(Cookie::make('name', 'value', $minutes));
    
  • You can then retrieve the data in the subsequent request

    $value = Cookie::get('name');
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on how to create a zip download feature with the click of a button

I am struggling to add a button in front of each tr on my table. When clicked, the button should trigger a download dialog box for the file. Despite seeing this feature often, I am unsure how to implement it. Currently, I have set up the button within a fo ...

Having issues with PDOStatement::nextRowset() functionality when using dblib driver with MSSQL database

The Challenge After transitioning my web application from a Windows Server to a Linux Server, I made significant adjustments. One of the changes involved altering how I connect with my MSSQL server. Previously, I used the Windows PDO_SQLSRV driver for da ...

When combining data from two tables using SQL, PHP fails to display the merged values on the table

Apologies for the inconvenience, but I am spending too much time on this: My SQL query is returning the table as expected, but unfortunately I can't seem to display the result on the table. This is my SQL code: $ssql = "SELECT * FROM tickets JOIN (S ...

Is there a more effective method than utilizing AppEntity as an alias E?

Is there a more efficient way to load all entities at once without having to load them one by one? I have over 20 entities for my project, and it's time-consuming to write out all the names and code needed on the page. The file header currently looks ...

The function ajax does not recognize response.forEach as a valid function

When I try to use ajax for fetching data from MySQL using PHP and JavaScript, I encounter a function error stating "response.forEach is not a function". Despite looking through various posts on this issue, none of the suggested solutions have been able to ...

What can I do to ensure that ob_start() functions properly in conjunction with curl?

In one of my classes, I have implemented the following methods: public function __construct(){ $this->handle = curl_init(); } public function setOptArrayAndExecute(){ $curlArray = curl_setopt_array( $this->handle, a ...

Show the most recent image from a folder in real-time

Looking for a solution to automatically display the latest image from a folder without refreshing the page? Check out this code snippet that achieves just that: <?php foreach (glob('/home/pi/camera/*.jpg') as $f) { $list[] = $f; } sort( ...

Consider utilizing the return statement in a PHP function by incorporating an if statement

Can you incorporate a return statement in a function along with an if statement? I want to know if the function was executed up to the if statement or not. This would help me verify if the SQL query will also be executed. Although I understand that I can ...

Is there a way to automatically load a package's classes from inside the package?

The structure of my application, which includes a composer package, is as shown below: some-application ├── index.php └── composer.json └── vendor/my/package ├── composer.json └── src ...

Troubleshooting imagejpeg() Function Failure in PHP Server

I've been working on implementing image cropping functionality for my website. I've managed to send an array of cropped image dimensions (x, y, width, height) to my PHP script. On my localhost, the script successfully crops the image, but unfort ...

Looking to design an interactive grid for generating dynamic thumbnails

I am a beginner in the field of web development and I have a desire to create a website for showcasing my portfolio. This website should feature project thumbnails along with brief descriptions, all of which should be displayed dynamically. Although I poss ...

Guide to choosing an attribute using wildcard string search in PHP's SimpleXML library

I am struggling to figure out how to select nodes with an attribute that contains the word "Rock" using PHP's SimpleXML library. I attempted to add a wild-card character to the attribute value, but it did not work as expected. Below is a snippet from ...

How to Create an Alert Message with Title and Dismiss Button in Joomla

After receiving a response from an ajax call, I am trying to display a Joomla notice. Here is the code: $app = JFactory::getApplication(); $app->enqueueMessage('Joomla notice', 'info'); When rendered on the front end, it appears as ...

Can you explain the concept of a templating language to me?

As I delved into my research, an interesting topic came up regarding PHP's role as a templating language. What does it mean for a language to be classified as a templating language? What specific features or characteristics define PHP as such? Further ...

Having trouble installing Composer globally on Cygwin

When executing the command 'php -r "readfile('');" | php' in a Cygwin terminal, the following errors appeared: Some configurations on your system are causing Composer to malfunction. Ensure you address the issues listed below and rerun ...

Retrieve several values with a limit of 1 in SQL

Consider the following table structure: ID | ident | product 1 | cucu1 | 99867 | 2 | kkju7 | 88987 | 3 | sjdu4 | 66754 | 4 | kjhu6 | 76654 | 5 | cucu1 | 98876 | Running the query: SELECT ident,COUNT(*) FROM sales WHERE status=? AND team=? AND DATE(da ...

Error encountered while making an ajax request in Laravel

I've created a basic live chat feature that functions smoothly, however, I'm encountering an issue where the ajax request occasionally returns a 500 (Internal Server Error) and fails to complete. Upon checking Laravel.log, I found the following ...

Deactivating distance-based shipping method in SilverStripe SilverShop

My ecommerce site is powered by the SilverShop add-on and utilizes SilverStripe Shipping. However, I encounter an error when trying to run it on a remote server, specifically on the production copy of the site. The error seems to be related to silvershop/g ...

Using Laravel for login with the option to choose from multiple usernames

I am looking to enhance my Laravel VueJS application by allowing users to log in using their username and registration number instead of just an email. I have successfully implemented this functionality by modifying the username function within the Authent ...

sending information to PHP through AJAX dynamically

I implemented a registration form that utilizes function user_reg(user_name,user_email,user_pswd) { var serverpath=window.location; alert(serverpath); var dataString = 'name='+ user_name + '&email=' + user_email + '&psw ...