'compact(): Undefined variable: operator
I'm getting the following error
(1/1) ErrorException
compact(): Undefined variable: operator
This is my line of code
$postsCat = Post::whereHas('Cat', function($query) use ($sreachWord) {
return $query->whereRaw('name REGEXP"'.sql_text_to_regx($sreachWord).'"');
})->orderBy('top','desc')
->orderBy('updated_at','desc')
->paginate(30);
Why is this happening? Is it because of my PHP version (7.3) or something else?
Solution 1:[1]
Please refer to this https://github.com/laravel/framework/issues/26936
The version of Laravel would need to be updated.
Solution 2:[2]
There are 2 fixes for this issue
- Downgrade your php to 7.2
- run "composer update" as in latest Laravel this issue has been resolved.
Solution 3:[3]
Instead of passing the variable to the compact() method, you'll passe it the name of the variable as a string without the dollar symbol.
$postsCats = Post::all(); // example
return view('posts.index', compact('postsCats'));
Solution 4:[4]
If you are not able to upgrade your Laravel, you just could change your Query to RAW query, it worked for me.
Solution 5:[5]
$posts = Post::latest()->get();
return view('author.post.index', compact('posts'));
Solution 6:[6]
Latest PHP version doesn't allow use of undefined variables. Instead of removing the latest version, another option is to switch between versions. Install earlier version say PHP7.2 as outlined here. Then set this as the preferred version by running sudo update-alternatives --set php /usr/bin/php7.2 on your Ubuntu terminal. Then run composer update
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eaststrong |
| Solution 2 | Tousif |
| Solution 3 | faye.babacar78 |
| Solution 4 | Josue Molina Blas |
| Solution 5 | Md.Azizur Rahman |
| Solution 6 | Elated Coder |
