Skip to content

Commit

Permalink
编辑个人资料
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 19, 2019
1 parent 7590678 commit c84cc3a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@

use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests\UserRequest;

class UsersController extends Controller
{
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', '个人资料更新成功!');
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;

class UserRequest extends FormRequest
{
public function authorize()
{
return true;
}

public function rules()
{
return [
'name' => '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' => '用户名不能为空。',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddAvatarAndIntroductionToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('avatar')->nullable();
$table->string('introduction')->nullable();
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('avatar');
$table->dropColumn('introduction');
});
}
}
4 changes: 2 additions & 2 deletions resources/views/layouts/_header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="">个人中心</a>
<a class="dropdown-item" href="">编辑资料</a>
<a class="dropdown-item" href="{{ route('users.show', Auth::id()) }}">个人中心</a>
<a class="dropdown-item" href="{{ route('users.edit', Auth::id()) }}">编辑资料</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" id="logout" href="#">
<form action="{{ route('logout') }}" method="POST">
Expand Down
10 changes: 10 additions & 0 deletions resources/views/shared/_error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@if (count($errors) > 0)
<div class="alert alert-danger">
<div class="mt-2"><b>有错误发生:</b></div>
<ul class="mt-2 mb-2">
@foreach ($errors->all() as $error)
<li><i class="glyphicon glyphicon-remove"></i> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
44 changes: 44 additions & 0 deletions resources/views/users/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@extends('layouts.app')

@section('content')

<div class="container">
<div class="col-md-8 offset-md-2">

<div class="card">
<div class="card-header">
<h4>
<i class="glyphicon glyphicon-edit"></i> 编辑个人资料
</h4>
</div>

<div class="card-body">

<form action="{{ route('users.update', $user->id) }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

@include('shared._error')

<div class="form-group">
<label for="name-field">用户名</label>
<input class="form-control" type="text" name="name" id="name-field" value="{{ old('name', $user->name) }}" />
</div>
<div class="form-group">
<label for="email-field">邮 箱</label>
<input class="form-control" type="text" name="email" id="email-field" value="{{ old('email', $user->email) }}" />
</div>
<div class="form-group">
<label for="introduction-field">个人简介</label>
<textarea name="introduction" id="introduction-field" class="form-control" rows="3">{{ old('introduction', $user->introduction) }}</textarea>
</div>
<div class="well well-sm">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</form>
</div>
</div>
</div>
</div>

@endsection

0 comments on commit c84cc3a

Please sign in to comment.