0

I'm trying to create a variable accessible by two different controller functins in laravel. How can I do that. The first function gets a value from a blade, it stores it in a variable and then I want to pass that variable with value to another controller function. For example, the following blade passes obj_id to controller:

1) My blade:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<title>test</title>
</head>
<body>
    <form method='post' action="/hard">
        {{csrf_field()}}
        <br>
        <legend><i> Fill Data </i></legend>
        <br>
        <label>
            OBJECT ID:
            <input name='obj_id' type='text' minlength="8" required="" oninvalid="this.setCustomValidity('8 char at least')">
        </label>
        <br>

        <input type='submit' value="Submit!">
    </form>
    <br>
    <br>

</body>
</html>

2) My controller function Roger correctly gets obj_id (I have tested ot with dd)

public function Roger(Request $p)
{
    $t = $p-> get('obj_id'); //I want $t to be global variable
    //dd($t);
}

3) and then I want to pass $t to function Roger1 in the same controller

public function Roger1()
{
    dd($t);
}

I have tried to declare $t as global with no success. I'm a little bit confused with $this and tried several combinations with no success.

Could you assist please?

loic.lopez
  • 2,013
  • 2
  • 21
  • 42
Konstantinos
  • 51
  • 1
  • 14

2 Answers2

1

You can use the session to store a variable

public function Roger(Request $p)
{
    $t = $p-> get('obj_id'); //I want $t to be global variable
    $p->session()->put('myvalue', $t);
}

public function Roger1(Request $p)
{
    $p->session()->get('myvalue);
}

https://laravel.com/docs/5.8/session#storing-data

Omar Makled
  • 1,638
  • 1
  • 19
  • 17
  • Hi Omar. I followed your code but although I don't get an error, I'm not getting what I was expecting. I just want to have a global variable and assign it a value inside one function and exactly the same variable with value to be accessible by another function in the same controller. – Konstantinos Apr 21 '19 at 14:40
  • check this out might be helpful for you but as I wrote store in session might fit your needs https://stackoverflow.com/questions/25189427/global-variable-for-all-controller-and-views – Omar Makled Apr 21 '19 at 15:53
0

Scenatio #01

IF both methods are in the same controller AND your second method is called inside the first method (in the same call), you can just do:

class CoolController extends Controller {

    public $var;

    public function first_method(Request $value)
    {
        // Example 1: passing the value as a parameter:
        $this->second_method($value);

        // Example 2: passing the value through a class variable:
        $this->var = $value; // $value: 'some-text'
        $this->third_method();
    }

    public function second_method($value)
    {
        dd($value); // 'some-text'
    }

    public function third_method()
    {
        dd($this->var); // 'some-text';
    }

}

Scenatio #02

Now, in the case you want to make a request from your view to set a value in your first method, and then another request calling your second method and getting that value that was "stored" in the first call.. well you can use any of this approaches. Why? because both call are in different lifecycles.

When the first call ended the value assigned in the first method (stored in memory) will be erased when the request is finished. That's why your second call will get a null value if you try to use it.

To store a temporary variable you have several paths:

  • Store it in the database.
  • Store it in the cache.
  • Send the value as a request parameter when doing the second call.
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • Hi HCK! Thanks for your advice. But no, the second method is not called inside the first. I hust want to assign a value to a variable inside the first method, and then to have that variable with its value available to the second method. Just as in my (2) and (3) code excerpts... – Konstantinos Apr 21 '19 at 14:44
  • @Konstantinos I also answer that possibility in the second part of my answer, read it till the end. – Kenny Horna Apr 21 '19 at 14:47
  • I'm familiar with database use in laravel and I'll probably do it this way. I think the third option does not fit with my code, but I'll check the cache case. Thanks a lot, anyway. – Konstantinos Apr 22 '19 at 05:15