konrms
6 months ago

Global variable in controller

Posted 6 months ago by konrms

Hello guys.

I want to create a varible which should be available in two different controller functions. How can I do that?

For example, the first function gets data from a blade and I want to pass it to another function in the same controller. My code sample is this:

  1. The blade which sends obj_id:
<!DOCTYPE html>

<html>
<meta charset="UTF-8">
<head>

<title>test</title>
</head>


<body>

    <form method='post' action="/hard">
        {{csrf_field()}}
        <br>
        <legend><i> fill number </i></legend>
        <br>
        <label>
            OBJECT ID:
            <input name='obj_id' type='text' minlength="8" required="" oninvalid="this.setCustomValidity('At least 8 characters must be used)">
        </label>
        <br>

        <input type='submit' value="Υποβολή!">
    </form>
    <br>
    <br>
    
</body>
</html>
  1. This is my controller function Roger which gets obj_id from blade.
public function Roger(Request $p)
    {
        $t = $p-> get('obj_id');   //I want $ to be available to other controller functions
            
    }
  1. How to pass $t to another function (e.g. Roger1) in the same blade and just dd it?
public function Roger1()
    {
        
        dd(t);
    }

I have tried to declade global t but didn't work.

Thanks a lot!

Please sign in or create an account to participate in this conversation.