Php Laravel刀片@can策略-字符串

Php Laravel刀片@can策略-字符串,php,laravel,laravel-5,laravel-5.2,laravel-authorization,Php,Laravel,Laravel 5,Laravel 5.2,Laravel Authorization,我使用的是Laravel5.2。所以我正在学习如何处理角色和权限。一切正常。我甚至制定了自己的政策 现在来谈谈问题。我将$post数据加载到PostsController中的视图中,然后加载到blade中 PostsController: public function show($id) { $post = Post::find($id); return view('posts.show', compact('post')); } @section('content') &

我使用的是Laravel5.2。所以我正在学习如何处理角色和权限。一切正常。我甚至制定了自己的政策

现在来谈谈问题。我将$post数据加载到PostsController中的视图中,然后加载到blade中

PostsController

public function show($id)
{
    $post = Post::find($id);

    return view('posts.show', compact('post'));
}
@section('content')
<!-- begin -->
@can('hasRole', Auth::user())
    <h1>Displaying Admin content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying moderator content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying guest content</h1>
@endcan
posts/show.blade.php

public function show($id)
{
    $post = Post::find($id);

    return view('posts.show', compact('post'));
}
@section('content')
<!-- begin -->
@can('hasRole', Auth::user())
    <h1>Displaying Admin content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying moderator content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying guest content</h1>
@endcan
现在返回所有内容。

当我更改
@can('hasRole',Auth::user())
从…起 将Auth::user()转换为字符串,即

@can('hasRole', 'guest')
    <h1>Displaying guest content</h1>
@endcan
@can('hasRole','guest')
显示来宾内容
@恩德坎

在这种情况下,它不会返回任何内容。因为我是拉威尔的新手,我真的不知道它不起作用

您可能没有足够仔细地阅读文档。您应该将模型作为第二个参数传递,而不是字符串或用户对象。在您的情况下,您可能应该使用以下内容:

@section('content')
<!-- begin -->
@can('hasRole', $post)
    <h1>Displaying Admin content</h1>
@endcan

@can('hasRole', $post)
    <h1>Displaying moderator content</h1>
@endcan

@can('hasRole', $post)
    <h1>Displaying guest content</h1>
@endcan
现在您可以在刀片中使用:

@section('content')
<!-- begin -->

@if (auth()->check())    
    @if (auth()->user()->hasRole('admin'))
        <h1>Displaying Admin content</h1>       
    @elseif (auth()->user()->hasRole('moderator'))
        <h1>Displaying moderator content</h1>
    @endif    
@else
    <h1>Displaying guest content</h1>
@endif
@节(“内容”)
@如果(auth()->check())
@如果(auth()->user()->hasRole('admin'))
显示管理内容
@elseif(auth()->user()->hasRole('moderator'))
显示版主内容
@恩迪夫
@否则
显示来宾内容
@恩迪夫

您可能没有足够仔细地阅读文档。您应该将模型作为第二个参数传递,而不是字符串或用户对象。在您的情况下,您可能应该使用以下内容:

@section('content')
<!-- begin -->
@can('hasRole', $post)
    <h1>Displaying Admin content</h1>
@endcan

@can('hasRole', $post)
    <h1>Displaying moderator content</h1>
@endcan

@can('hasRole', $post)
    <h1>Displaying guest content</h1>
@endcan
现在您可以在刀片中使用:

@section('content')
<!-- begin -->

@if (auth()->check())    
    @if (auth()->user()->hasRole('admin'))
        <h1>Displaying Admin content</h1>       
    @elseif (auth()->user()->hasRole('moderator'))
        <h1>Displaying moderator content</h1>
    @endif    
@else
    <h1>Displaying guest content</h1>
@endif
@节(“内容”)
@如果(auth()->check())
@如果(auth()->user()->hasRole('admin'))
显示管理内容
@elseif(auth()->user()->hasRole('moderator'))
显示版主内容
@恩迪夫
@否则
显示来宾内容
@恩迪夫

The docs you link to state“为方便起见,Laravel提供了
@can
Blade指令,以快速检查当前已验证的用户是否具有给定的能力。”“为方便起见,Laravel提供了
@can
Blade指令,以快速检查当前已认证的用户是否具有给定的能力。”感谢您提供的详细答案。感谢您提供的详细答案。