33

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?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Kareimovich
  • 591
  • 1
  • 7
  • 18

7 Answers7

44

Go to your project in

vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php

On line number 1337, you can found below code inside the addWhereExistsQuery method

$this->wheres[] = compact('type', 'operator', 'query', 'boolean');

You just remove the 'operator' parameter.

And I hope it will work fine.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Thank you, @Hasanur. In my case the defective code was found on line 1229 of `Builder.php`. After studying that code I removed `operator` as you recommend and the call to `addWhereExistsQuery()` ran successfully. However, I think you should add that this is a workaround only for this particular instance of the larger issue reported here: that of PHP 7.3+ rejecting undefined variables. – CODE-REaD Feb 21 '20 at 20:57
  • yesssssss, just remove the 'operator' paramiter ,,,, thanks a lot – saber tabatabaee yazdi Jun 03 '20 at 00:12
  • Thanks a lot... you have saved my day.. @Hasanur Kareimovich you should accept it if work for you too. – MD Alauddin Al-Amin Aug 05 '20 at 19:17
  • 6
    This is hacky solution, and should be used only if it is not possible to update Laravel version for some reason. – Fusion Jan 10 '21 at 23:01
  • 2
    @Hasanur Rahman Himel . I have also this issue , exploring from last two hours . Here is exact solution I found . Thanks for saving my time. In my case it is 1229 line. – Assad Yaqoob Feb 18 '21 at 06:19
  • 1
    this is not the right way as the given file can be overwrite with any update. "composer update" will solve this issue – Tousif Feb 23 '21 at 10:49
  • @AkashSethi The solution doesn't work in Laravel 8, the parameter isn't there anymore, and the problem persists. Have you stumbled upon an updated solution? – Peter Jun 14 '21 at 00:41
  • I have did that but not Perfect solution because error went to gammer.php in same word operator.....! – Mostafa Mahmoud Sep 24 '21 at 13:01
  • 1
    No, no, no. _Never_ modify any files within the **vendor** directory! This is terrible advice, and don’t know why it has been upvoted as many times as it has. – Martin Bean Jul 05 '22 at 22:23
7

Please refer to this https://github.com/laravel/framework/issues/26936

The version of Laravel would need to be updated.

eaststrong
  • 373
  • 1
  • 4
  • 7
6

There are 2 fixes for this issue

  1. Downgrade your php to 7.2
  2. run "composer update" as in latest Laravel this issue has been resolved.
Tousif
  • 301
  • 3
  • 11
4

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'));
faye.babacar78
  • 774
  • 7
  • 12
0

If you are not able to upgrade your Laravel, you just could change your Query to RAW query, it worked for me.

0
  $posts = Post::latest()->get();
            return view('author.post.index', compact('posts'));
  • 3
    While your code snippet may answer the question, providing additional context regarding why and/or how your code snippet works improves its long-term value. – Sven Eberth Jun 14 '21 at 13:23
0

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

Elated Coder
  • 312
  • 1
  • 16