【问题标题】:How get correct values from DB in Laravel 5.8如何从 Laravel 5.8 中的 DB 获取正确的值
【发布时间】:2019-10-12 11:24:21
【问题描述】:

我是 Laravel 的初学者。我有这个代码:

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}}]

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    多对多关系,每个用户可能有多个角色!因此,请使用 foreach 打印 blade 中的所有规则:

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

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-18
      • 2018-12-30
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多