根据用户权限隐藏模型字段

Hide model fields based on user permissions

假设 user_one 有权 查看 purchase_price, 而user_two没有这个权限。

我想根据用户权限动态隐藏purchase_price

What I have done

我已经使用 spatie 包管理权限。

What I have tried and searched for

您可以创建策略,然后在控制器中使用 can() 并在 blade 文件中使用 @can 作为角色过滤器。 Click here 获取更多信息。

if ($this->getAuthorisedApp()->cannot('purchase_price'))
    {
        $this->setHidden(['purchase_price']);

        //  Or, $this->setVisible(['example_key']), if this works better for you.
    }