gear
Image not found

How to get another method data in same controller in laravel?

Comment (0)

Admin


In Laravel, you can access data from another method within the same controller using class properties. Here's a simple example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class YourController extends Controller
{
    // Define a property to store data
    private $someData;

    public function firstMethod()
    {
        // Set data in the property
        $this->someData = 'Hello from firstMethod!';
        return $this->secondMethod();
    }

    public function secondMethod()
    {
        // Access data from the property
        $data = $this->someData;
        return view('your.view', compact('data'));
    }
}

 

In this example, the firstMethod sets data in the $someData property, and then it calls secondMethod. The secondMethod can access the data from the property. 

Remember to adjust this based on your specific use case and requirements. If you need to share data between methods within a request lifecycle, using properties within the controller is a common approach.

Hope it wil help....

Thanks


Others Problem Fix Stroy



Comments (0)

Your Comment