0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel8】App\Models\User::isAdmin must return a relationship instance. の解決策

Posted at

App\Models\User::isAdmin must return a relationship instance.
のエラーが出たので対処法を覚書。

エラーが出た時の実際のコード

app/Providers/AuthServiceProvider.php
    public function boot()
    {
        $this->registerPolicies();

        Gate::define('admin', function ($user) {
            return $user->isAdmin;
        });
    }
app/Models/User.php
    public function isAdmin()
    {
        return $this->role === self::ROLE_ADMIN;
    }

結論

app/Providers/AuthServiceProvider.php
    public function boot()
    {
        $this->registerPolicies();

        Gate::define('admin', function ($user) {
            return $user->isAdmin(); // ここの()がなかった
        });
    }

isAdminの() がないだけでした。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?