## 本节说明 * 对应视频教程第 86 小节:An Administrator May Lock Any Thread: Part 3 ## 本节内容 在之前的章节中,我们定义了用户名为 NoNo1 的用户为管理员,但是谁都能注册为 NoNo1。所以本节开始之前我们首先来为用户名加上唯一性限制: *forum\database\migrations\2014_10_12_000000_create_users_table.php* ``` . . public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->string('email')->unique(); $table->string('password'); $table->string('avatar_path')->nullable(); $table->boolean('confirmed')->default(false); $table->string('confirmation_token',25)->nullable()->unique(); $table->rememberToken(); $table->timestamps(); }); } . . ``` 运行迁移: ``` $ php artisan migrate:refresh ``` 现在我们继续锁定话题功能的开发,本节我们来实现锁定功能前端效果。首先我们加入前端管理员的判定: *forum\resources\assets\js\authorizations.js* ``` let user = window.App.user; module.exports = { owns (model,prop = 'user_id') { return model[prop] == user.id; }, isAdmin() { return ['NoNo1'].includes(user.name); } }; ``` 接着我们来修改话题页面: *forum\resources\views\threads\show.blade.php* ``` . . // 绑定 data-locked 属性 . . // 增加 Lock 按钮 Lock . . @endsection ``` 修改`Thread.vue`组件,接收绑定的属性: *forum\resources\assets\js\pages\Thread.vue* ``` ``` 我们设定的是,当话题被锁定时无法进行回复,所以我们还要设定`NewReply.vue`组件在话题被锁定时不可见: *forum\resources\assets\js\components\Replies.vue* ``` . . This thread is locked.No more replies are allowed. . . ``` 查看效果: ![file](https://cdn.learnku.com/uploads/images/201807/01/19192/12sRb7V1zG.gif?imageView2/2/w/1240/h/0)
// 增加 Lock 按钮 Lock
This thread is locked.No more replies are allowed.