如何从Laravel 5.8中的DB获取正确的值

时间:2019-05-26 19:22:49

标签: php laravel laravel-5

我是Laravel的乞gg。我有以下代码:

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use psCMS\Presenters\UserPresenter;

    public static $roles = [];


    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

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


    public function hasRole(array $roles)
    {

        foreach($roles as $role)
        {

            if(isset(self::$roles[$role]))
            {
                if(self::$roles[$role])  return true;

            }
            else
            {
                self::$roles[$role] = $this->roles()->where('name', $role)->exists();
                if(self::$roles[$role]) return true;
            }

        }
        return false;
    }

}

class Role extends Model
{
    protected $quarded = [];
    public $timestamps = false;

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

}

和架构:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->boolean('enable')->default(0);
            $table->string('name', 120)->nullable();
            $table->string('surname', 120)->nullable();
            $table->string('email', 120)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->bigInteger('counter')->default(0);
            $table->rememberToken();
            $table->timestamps();
            $table->engine = "InnoDB";
        });


Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->engine = "InnoDB";
        });



Schema::create('role_user', function (Blueprint $table) {
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->bigInteger('role_id')->unsigned();
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->engine = "InnoDB";
        });

我的用户

DB::table('users')->insert([
            'name' => 'Marian',
            'surname' => 'La',
            'email' => 'marian@icloud.com',
            'email_verified_at' => \Carbon\Carbon::now(),
            'password' => Hash::make('passw'),
        ]);       

DB::table('role_user')->insert([
                'user_id' => 1,
                'role_id' => 1,
            ]);

此代码可以正常工作。我的角色有问题。 如何在刀片中打印用户角色?

我输入以下代码:

public function getAdmin(int $id)
    {
        return User::find($id);
    }

$admin = $this->getAdmin(1);

现在我的$ admin-具有管理对象。

当我在刀片文件中打印时:$ admin-> name,$ admin-> surname-有效。

当我打印时:{{$ admin-> roles}}

我有结果:

[{"id":1,"name":"admin","pivot":{"user_id":1,"role_id":1}}]

如何显示正确的值(管理员):

我需要结果:管理员不是这个:     [{{id“:1,” name“:” admin“,” pivot“:{” user_id“:1,” role_id“:1}}]

1 个答案:

答案 0 :(得分:1)

这是多对多关系,每个用户可能有许多角色!因此,使用foreach将所有规则打印在刀片中:

@foreach ($admin->roles as $role)
  {{ $role -> name }},
@endforeach
  

https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

希望这会有所帮助!