T
T
Tyoma Inagamov2019-12-14 17:45:19
Laravel
Tyoma Inagamov, 2019-12-14 17:45:19

How to pass a variable from one controller function to another and call it from a file with routes?

I want to pass a variable from one public function to another (in the controller class) and call this function (with the passed variable) in the routes file in order to use this variable as a url there.
So, I have myController, and I want to pass $Hashfrom the first function to the second. I also want to make a route where I will use the function variable second$Hashas the url.
Here is what I did but it doesn't work.
( Firstly , I have an error:

Too few arguments to function App\\Http\\Controllers\\myController::second(), 1 passed and exactly 2 expected

And secondly , I'm not very sure about what I wrote in the
Controller routes
class myController extends Controller
{
    public function first(Request $request, $Hash)
    {
            $requested_email = $request->email;
            $getUserByEmail = DB::table('users')->where('email', $requested_email)->first();
            $Hash = Crypt::encrypt($getUserByEmail->password);

            echo $Hash;
    }

    public function second($Hash)
    {
        echo $Hash;
    }
    
}

Routes PS: Of course I have to get the user's e-mail (and => and all his data from the db)
Route::get('{Hash}', '[email protected]');
route::post('url', '[email protected]')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
photozoom, 2020-02-04
@slower

And if you declare a variable in the controller before the functions? and then in the first function:
private $Hash = '';

public function first(Request $request, $Hash)
    {
            $requested_email = $request->email;
            $getUserByEmail = DB::table('users')->where('email', $requested_email)->first();
            $this->Hash = Crypt::encrypt($getUserByEmail->password);

            echo $this->Hash;
    }

and for the second time:
public function second($this->Hash)
    {
        echo $this->Hash;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question