Global variable in controller 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:
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>
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
}
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!
you would be best to study and understand the request lifecycle
I want $ to be available to other controller functions
Above I didn't know if you always meant in the same controller, or other controllers as well.
@JLRDW - Hi jlrdw.
Yes, I want to be transferred to another function in the same controller only.
If I add the following line to Roger
$this -> Roger1($p);
I get this (?):
Request {#42 ▼
#json: null
#convertedFiles: null
#userResolver: Closure($guard = null) {#211 ▶}
#routeResolver: Closure() {#213 ▶}
+attributes: ParameterBag {#44 ▶}
+request: ParameterBag {#43 ▶}
+query: ParameterBag {#50 ▼
#parameters: []
}
+server: ServerBag {#46 ▶}
+files: FileBag {#47 ▶}
+cookies: ParameterBag {#45 ▶}
+headers: HeaderBag {#48 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/hard"
#requestUri: "/hard"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: Store {#248 ▼
#id: "y3kO9bIBPS3WiQIwyynz27yO5mbFBUHWZ7vyBnJ6"
#name: "laravel_session"
#attributes: array:3 [▶]
#handler: FileSessionHandler {#249 ▶}
#started: true
}
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
My solution (may not the be best but worked fine for me) was to store variable data into a database table and then request this data twice via the same query inside each function.
Please sign in or create an account to participate in this conversation.