简体   繁体   English

"compact():未定义变量:运算符"

[英]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?是因为我的 PHP 版本(7.3)还是其他原因?

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在第 1337 行,您可以在addWhereExistsQuery方法中找到以下代码

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

You just remove the 'operator' parameter.您只需删除 'operator' 参数。

And I hope it will work fine.我希望它能正常工作。

Please refer to this https://github.com/laravel/framework/issues/26936请参考这个https://github.com/laravel/framework/issues/26936

The version of Laravel would need to be updated. Laravel 的版本需要更新。

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.不是将变量传递给compact()方法,而是将变量的名称作为不带美元符号的字符串传递给它。

$postsCats = Post::all(); // example

return view('posts.index', compact('postsCats'));

There are 2 fixes for this issue此问题有 2 个修复程序

  1. Downgrade your php to 7.2将你的 php 降级到 7.2
  2. run "composer update" as in latest Laravel this issue has been resolved.在最新的 Laravel 中运行“composer update”,这个问题已经解决。

如果您无法升级您的 Laravel,您只需将您的查询更改为 RAW 查询,它对我有用。

  $posts = Post::latest()->get();
            return view('author.post.index', compact('posts'));

Latest PHP version doesn't allow use of undefined variables.最新的 PHP 版本不允许使用未定义的变量。 Instead of removing the latest version, another option is to switch between versions.另一个选择是在版本之间切换,而不是删除最新版本。 Install earlier version say PHP7.2<\/strong> as outlined here.<\/a>安装较早的版本,如此处概述的PHP7.2<\/strong> 。<\/a> Then set this as the preferred version by running sudo update-alternatives --set php \/usr\/bin\/php7.2<\/code> on your Ubuntu terminal.然后通过在 Ubuntu 终端上运行sudo update-alternatives --set php \/usr\/bin\/php7.2<\/code>将其设置为首选版本。 Then run composer update<\/code>然后运行composer update<\/code>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM