From c84cc3ae9c658082121ec9442b68b2036b73003a Mon Sep 17 00:00:00 2001 From: Summer Date: Thu, 19 Sep 2019 11:01:16 +0000 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E4=B8=AA=E4=BA=BA=E8=B5=84?= =?UTF-8?q?=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/UsersController.php | 12 +++++ app/Http/Requests/UserRequest.php | 33 ++++++++++++++ ...avatar_and_introduction_to_users_table.php | 24 ++++++++++ resources/views/layouts/_header.blade.php | 4 +- resources/views/shared/_error.blade.php | 10 +++++ resources/views/users/edit.blade.php | 44 +++++++++++++++++++ 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 app/Http/Requests/UserRequest.php create mode 100644 database/migrations/2019_09_19_185528_add_avatar_and_introduction_to_users_table.php create mode 100644 resources/views/shared/_error.blade.php create mode 100644 resources/views/users/edit.blade.php diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index c9e9b45b7..ff7b08fb9 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use App\Models\User; +use App\Http\Requests\UserRequest; class UsersController extends Controller { @@ -11,4 +12,15 @@ public function show(User $user) { return view('users.show', compact('user')); } + + public function edit(User $user) + { + return view('users.edit', compact('user')); + } + + public function update(UserRequest $request, User $user) + { + $user->update($request->all()); + return redirect()->route('users.show', $user->id)->with('success', '个人资料更新成功!'); + } } diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php new file mode 100644 index 000000000..9122f1ad5 --- /dev/null +++ b/app/Http/Requests/UserRequest.php @@ -0,0 +1,33 @@ + 'required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . Auth::id(), + 'email' => 'required|email', + 'introduction' => 'max:80', + ]; + } + + public function messages() + { + return [ + 'name.unique' => '用户名已被占用,请重新填写', + 'name.regex' => '用户名只支持英文、数字、横杠和下划线。', + 'name.between' => '用户名必须介于 3 - 25 个字符之间。', + 'name.required' => '用户名不能为空。', + ]; + } +} diff --git a/database/migrations/2019_09_19_185528_add_avatar_and_introduction_to_users_table.php b/database/migrations/2019_09_19_185528_add_avatar_and_introduction_to_users_table.php new file mode 100644 index 000000000..ec03880b0 --- /dev/null +++ b/database/migrations/2019_09_19_185528_add_avatar_and_introduction_to_users_table.php @@ -0,0 +1,24 @@ +string('avatar')->nullable(); + $table->string('introduction')->nullable(); + }); + } + + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('avatar'); + $table->dropColumn('introduction'); + }); + } +} diff --git a/resources/views/layouts/_header.blade.php b/resources/views/layouts/_header.blade.php index 4b6a20ed4..b25fc5d6c 100644 --- a/resources/views/layouts/_header.blade.php +++ b/resources/views/layouts/_header.blade.php @@ -27,8 +27,8 @@ {{ Auth::user()->name }}