资料修改优化,添加帖子分类
This commit is contained in:
parent
f249dd9f31
commit
bd4990974a
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Http\Requests\UserUpdate;
|
||||
use App\Models\User;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
class UsersController extends Controller
|
||||
{
|
||||
|
@ -14,14 +15,29 @@ class UsersController extends Controller
|
|||
|
||||
public function edit(User $user)
|
||||
{
|
||||
$this->authorize('update', $user);
|
||||
return view('users.edit', compact('user'));
|
||||
}
|
||||
|
||||
public function update(User $user, UserUpdate $request)
|
||||
{
|
||||
$this->authorize('update', $user);
|
||||
$attribute = $request->only(['name', 'email', 'introduction']);
|
||||
if ($request->file('avatar')) {
|
||||
$user->avatar = $request->file('avatar')->store('public/avatar');
|
||||
if ($image = $request->file('avatar')) {
|
||||
$user->avatar = $image->store('public/avatar');
|
||||
$file = Image::make(storage_path('app/' . $user->avatar));
|
||||
$file->resize(1280, null, function ($constraint) {
|
||||
|
||||
// 设定宽度是 $max_width,高度等比例双方缩放
|
||||
$constraint->aspectRatio();
|
||||
|
||||
// 防止裁图时图片尺寸变大
|
||||
$constraint->upsize();
|
||||
});
|
||||
|
||||
// 对图片修改后进行保存
|
||||
$file->save();
|
||||
|
||||
}
|
||||
$user->update($attribute);
|
||||
return redirect()->route('users.show', [$user])->with('success', '更新成功!');
|
||||
|
|
|
@ -31,7 +31,7 @@ class UserUpdate extends FormRequest
|
|||
],
|
||||
'email' => ['required', 'unique:users,email,' . Auth::id()],
|
||||
'introduction' => 'nullable|max:255',
|
||||
'avatar' => 'image|nullable',
|
||||
'avatar' => 'image|nullable|dimensions:min_width=200,min_height=200',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@ class UserUpdate extends FormRequest
|
|||
{
|
||||
return [
|
||||
'introduction' => '简介',
|
||||
'avatar' => '头像'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
17
app/Models/BaseModel.php
Normal file
17
app/Models/BaseModel.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: xing
|
||||
* Date: 2017/12/31
|
||||
* Time: 12:44
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BaseModel extends Model
|
||||
{
|
||||
|
||||
}
|
8
app/Models/Category.php
Normal file
8
app/Models/Category.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class Category extends BaseModel
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
}
|
27
app/Policies/UserPolicy.php
Normal file
27
app/Policies/UserPolicy.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class UserPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
/**
|
||||
* Create a new policy instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function update(User $currentUser, User $user)
|
||||
{
|
||||
return $currentUser->id === $user->id;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Policies\UserPolicy;
|
||||
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
||||
|
||||
class AuthServiceProvider extends ServiceProvider
|
||||
|
@ -13,6 +15,7 @@ class AuthServiceProvider extends ServiceProvider
|
|||
*/
|
||||
protected $policies = [
|
||||
'App\Model' => 'App\Policies\ModelPolicy',
|
||||
User::class => UserPolicy::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace App\Tools;
|
|||
|
||||
|
||||
use Illuminate\Http\File;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
class ImageUploadTool
|
||||
{
|
||||
|
@ -21,9 +22,10 @@ class ImageUploadTool
|
|||
* @param $file File
|
||||
* @param $folder
|
||||
* @param $file_prefix
|
||||
* @param bool $max_width
|
||||
* @return array|bool
|
||||
*/
|
||||
public function save($file, $folder, $file_prefix)
|
||||
public function save($file, $folder, $file_prefix, $max_width = false)
|
||||
{
|
||||
// 构建存储的文件夹规则,值如:uploads/images/avatars/201709/21/
|
||||
// 文件夹切割能让查找效率更高。
|
||||
|
@ -48,8 +50,34 @@ class ImageUploadTool
|
|||
// 将图片移动到我们的目标存储路径中
|
||||
$file->move($upload_path, $filename);
|
||||
|
||||
// 如果限制了图片宽度,就进行裁剪
|
||||
if ($max_width && $extension != 'gif') {
|
||||
|
||||
// 此类中封装的函数,用于裁剪图片
|
||||
$this->reduceSize($upload_path . '/' . $filename, $max_width);
|
||||
}
|
||||
|
||||
return [
|
||||
'path' => config('app.url') . "/$folder_name/$filename"
|
||||
];
|
||||
}
|
||||
|
||||
public function reduceSize($file_path, $max_width)
|
||||
{
|
||||
// 先实例化,传参是文件的磁盘物理路径
|
||||
$image = Image::make($file_path);
|
||||
|
||||
// 进行大小调整的操作
|
||||
$image->resize($max_width, null, function ($constraint) {
|
||||
|
||||
// 设定宽度是 $max_width,高度等比例双方缩放
|
||||
$constraint->aspectRatio();
|
||||
|
||||
// 防止裁图时图片尺寸变大
|
||||
$constraint->upsize();
|
||||
});
|
||||
|
||||
// 对图片修改后进行保存
|
||||
$image->save();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateCategoriesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('categories', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->index()->comment('名称');
|
||||
$table->text('description')->nullable()->comment('描述');
|
||||
$table->integer('post_count')->default(0)->comment('帖子数');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('categories');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class SeedCategoriesData extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$categories = [
|
||||
[
|
||||
'name' => '分享',
|
||||
'description' => '分享创造,分享发现',
|
||||
],
|
||||
[
|
||||
'name' => '教程',
|
||||
'description' => '开发技巧、推荐扩展包等',
|
||||
],
|
||||
[
|
||||
'name' => '问答',
|
||||
'description' => '请保持友善,互帮互助',
|
||||
],
|
||||
[
|
||||
'name' => '公告',
|
||||
'description' => '站点公告',
|
||||
],
|
||||
];
|
||||
\Illuminate\Support\Facades\DB::table('categories')->insert($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::table('categories')->truncate();
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@
|
|||
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
|
||||
<span class="user-avatar pull-left" style="margin-right:8px; margin-top:-5px;">
|
||||
<img src="{{Auth::user()->header}}"
|
||||
class="img-responsive img-circle" width="30px" height="30px">
|
||||
class="img-responsive img-circle" style=" max-height:30px">
|
||||
</span>
|
||||
{{ Auth::user()->name }} <span class="caret"></span>
|
||||
</a>
|
||||
|
|
|
@ -15,7 +15,8 @@ use Illuminate\Support\Facades\Route;
|
|||
|
||||
Route::get('/', 'PagesController@root')->name('home');
|
||||
|
||||
Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']]);
|
||||
Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']])
|
||||
->middleware('auth');
|
||||
|
||||
// Authentication Routes...
|
||||
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
|
||||
|
|
Loading…
Reference in New Issue
Block a user