laravel5.3开发知乎-第4章-发布问题 laravel5.3开发知乎-第4章-发布问题

2023-07-10

一、创建数据表

创建模型

php artisan make:model Question -m

修改生成数据迁移文件 questions_table.php

public function up()
{
   Schema::create('questions', function (Blueprint $table) {
       $table->increments('id');
       $table->string('title');
       $table->text('content');
       $table->integer('user_id')->unsigned();
       $table->integer('comments_count')->default(0);
       $table->integer('followers_count')->default(1);
       $table->integer('answers_count')->default(0);
       $table->string('close_comment', 8)->default('F');
       $table->string('is_hidden', 8)->default('F');
       $table->timestamps();
   });
}

执行数据迁移

 php artisan migrate

修改生成的模型:app/Question.php

protected $fillable = ['title', 'content', 'user_id'];

二、overtrue/laravel-ueditor

下载:https://github.com/overtrue/laravel-ueditor

composer require "overtrue/laravel-ueditor:~1.0"

添加下面一行到 config/app.php 中 providers 部分:

Overtrue\LaravelUEditor\UEditorServiceProvider::class,

发布配置文件与资源

php artisan vendor:publish --provider='Overtrue\LaravelUEditor\UEditorServiceProvider'

修改 public/vendor/ueditor/ueditor.config.js 的 toolbars

[[
   'fullscreen', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',  'pasteplain', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall',
   'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
   'customstyle', 'paragraph', 'fontfamily', 'fontsize', 'indent', '|',
   'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
   'link', 'unlink', 'anchor', '|',
   'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
   'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
   'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
   'preview', 'searchreplace', 'drafts'
]]

toolbars 参数说明:http://fex.baidu.com/ueditor/#start-toolbar

创建软链接

php artisan storage:link

图片上传七牛:https://github.com/overtrue/laravel-filesystem-qiniu

composer require "overtrue/laravel-filesystem-qiniu:^1.0"

添加下面一行到 config/app.php 中 providers 部分:

Overtrue\LaravelFilesystem\Qiniu\QiniuStorageServiceProvider::class,

修改 config/filesystems.php 的 disks 部分

'qiniu' => [
   'driver'     => 'qiniu',
   'access_key' => env('QINIU_ACCESS_KEY', 'xxxxxxxxxxxxxxxx'),
   'secret_key' => env('QINIU_SECRET_KEY', 'xxxxxxxxxxxxxxxx'),
   'bucket'     => env('QINIU_BUCKET', 'test'),
   'domain'     => env('QINIU_DOMAIN', 'xxx.clouddn.com'),
],

修改 .env 配置文件

QINIU_ACCESS_KEY=
QINIU_SECRET_KEY=
QINIU_BUCKET=
QINIU_DOMAIN=

修改 config/ueditor.php

'disk' => 'qiniu',

三、发布问题

创建表单验证

php artisan make:request StoreQuestionRequest

修改生成的文件:app/Http/Request/StoreQuestionRequest.php

public function authorize()
{
   return true;
}

public function messages()
{
   return [
       'title.required' => '标题不能为空',
       'title.min' => '标题至少3个字符',
       'title.max' => '标题最多3个字符',
       'content.required' => '内容不能为空',
       'content.min' => '内容至少20个字符',
   ];
}

public function rules()
{
   return [
       'title' => 'required|min:3|max:196',
       'content' => 'required|min:20'
   ];
}

创建控制器

php artisan make:controller QuestionsController --resource

修改 app/Http/Controllers/QuestionsController.php

use Auth;
use App\Question;

public function __construct()
{
   $this->middleware('auth')->except(['index','show']);
}

public function create()
{
   return view('questions.create');
}

public function store(StoreQuestionRequest $request)
{
   $data = [
       'title' => $request->get('title'),
       'content' => $request->get('content'),
       'user_id' => Auth::id()
   ];
   $question = Question::create($data);
   return redirect()->route('questions.show',[$question->id]);
}

public function show($id)
{
   $question = Question::find($id);
   return view('questions.show', compact('question'));
}

注册路由:routes/web.php

Route::resource('questions', 'QuestionsController');

新建视图:resources/views/questions/create.blade.php

https://file.lulublog.cn/images/3/2023/07/iDSzsT999tXs8r96SEbIsxxzmfnM9S.jpg

注意:vendor.ueditor.assets 必须在 js 后面

@section('js')
@include('vendor.ueditor.assets')

新建视图:resource/views/questions/show.blade.php

https://file.lulublog.cn/images/3/2023/07/WZ0VTxn9L1v1X8nn1vMff6L82T1Zpn.jpg

登录用户后访问:http://localhost:8000/questions/create

随便输入标题和内容,点击发布问题

https://file.lulublog.cn/images/3/2023/07/FX151Tg6H5k3z3M18yW63zTXYUAKJT.jpg

输入符合规则的标题和内容后

https://file.lulublog.cn/images/3/2023/07/sBmm75OmJml688A65B4a74aeMqmNoA.jpg

阅读 500