-3

i defined a public variable in laravel class then in search function i set this variable to the search data to use it in another function using $this->_lastSearch = $request->all() it didn't work, so i used a set function to set the value and a get function to get the value of the variable..

then in another function i used the get function to get the value of the variable as img below

code

code2

code3

i expected to get the value from set function

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Dark code
  • 1
  • 1
  • 1
    Welcome to SO ... please do not put up pictures of code; code is text and belongs in your question in a codeblock – lagbox Jan 02 '23 at 02:27
  • 1
    Do not share code or anything similar as images (except some errors) due to the image going down in a near future rendering this question unreadable and not understandable... – matiaslauriti Jan 02 '23 at 02:56
  • try to use dd() and show the results – UnderDog Jan 02 '23 at 04:01
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 02 '23 at 09:05

1 Answers1

0

A variable in an instance is not persistent. When you construct a new controller, it won't have the defined variable from another instance.

We can't see where you're using your getPaginatedSearchResult method, but most likely you're not calling it in the same lifecycle as the one where you're setting the $_lastSearch. Since it's a Laravel controller, you're probably calling it from a route (perhaps an AJAX request?), which means it will construct a new controller. If you wish to keep data persistent, use static variables. This answer shows how that works.

Yinci
  • 1,637
  • 1
  • 3
  • 14