1

我想在 Laravel 5.7 中注册一个新的全局作用域,但出现以下错误:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_PARSE)语法错误,意外'静态'(T_STATIC)

<?php

namespace App;

use Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    use SoftDeletes;

    /**
    * Anonymous scope
    */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('authenticated', function (Builder $builder) {
            $builder->where('id_user', '=', Auth::id());
        });
    }
}

我正在使用 laravel 5.7 PHP 7.2

4

3 回答 3

2

您正在尝试添加一个绝对没问题的匿名全局范围,但是您需要使用 Eloquent\Builder 才能使该方法起作用(这似乎不符合您的确切错误,但是,您将需要此),因此将以下内容添加到你的班级,看看错误是否改变!

use Illuminate\Database\Eloquent\Builder;
于 2018-11-28T14:20:14.590 回答
1

5.7 中全局范围的文档建议您应该以不同于您在这里的方式来实现它们。https://laravel.com/docs/5.7/eloquent#global-scopes

您需要实现Scope该类,然后创建一个apply()方法。

于 2018-11-28T13:54:18.640 回答
0

使用这个包生成全局作用域并将其加载到 laravel

https://github.com/limewell/laravel-make-extender

php artisan make:scope UserScope

php artisan make:scope ActiveScope

php artisan make:scope AgeScope

ETC...

于 2021-05-13T17:35:34.567 回答