laravel依赖项无法正常工作

时间:2017-05-22 19:24:07

标签: php laravel laravel-5.4

帮助理解,有一个普通的模型用户,关键是role_id:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function role()
    {
        return $this->hasOne('App\Role', 'id');
    }

    public function isManager()
    {
        return Auth::user()->role->name == 'manager';
    }

    public function isAdmin()
    {
        return Auth::user()->role->name == 'admin';
    }
}

角色模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'name'
    ];

    public function users()
    {
        return $this->hasMany('App\User');
    }
}

用户迁移表:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')
            ->unique();
        $table->string('password');
        $table->rememberToken();
        $table->integer('role_id')
            ->unsigned();
        $table->timestamps();
    });

    Schema::table('users', function (Blueprint $table) {
        $table->foreign('role_id')
            ->references('id')
            ->on('roles')
            ->onDelete('cascade');
    });
}

现在问题的关键。当我试图获得所有经理时,只有第一个显示角色。控制器:

public function managersView()
{
    $role = Role::where('name', 'manager')->first();
    $managers = Role::find($role->id)->users;
    return view('users.managers', [
        'managers' => $managers
    ]);
}

查看:

@foreach ($managers as $manager)
    <tr>
        <td>{{ $manager->id }}</td>
        <td>{{ $manager->name }}</td>
        <td>{{ $manager->role }}</td>
        <td>
            <button class="btn btn-success"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button>
            <button class="btn btn-warning"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
        </td>
    </tr>
@endforeach

该表仅显示第一个用户的角色。请帮助理解什么是不正确的?

2 个答案:

答案 0 :(得分:1)

我在模型设置方面遇到了一些问题。

在您的用户模型中,未正确设置外键。因为角色id存在于用户身上,所以它应该是belongsTo关系。密钥也应该是role_id,而不是id。它应该看起来像:

public function role()
{
    return $this->belongsTo('App\Role', 'role_id');
}

此外,您的角色检查方法应该引用当前模型,而不是当前授权的用户。无论您调用该方法的用户如何,您当前拥有该isManager()的方式始终会返回相同的内容。

public function isManager()
{
    return $this->role->name == 'manager';
}

public function isAdmin()
{
    return $this->role->name == 'admin';
}

通过这种方式,您可以检查任何用户的角色,而不仅仅是当前用户。如果要检查当前用户的角色,请使用Auth::user()->isAdmin()

答案 1 :(得分:1)

您应该在UsersController上更改此行:

public function role()
    {
        return $this->hasOne('App\Role', 'id');
    }

public function role()
    {
        return $this->belongsTo('App\Role', 'role_id');
    }