laravel5.3开发知乎-第8章-回答 laravel5.3开发知乎-第8章-回答

2023-07-12

一、创建数据表

创建模型

php artisan make:model Answer -m

修改生成的 create_answers_table.php

public function up()
{
   Schema::create('answers', function (Blueprint $table) {
       $table->increments('id');
       $table->integer('user_id')->index()->unsigned();
       $table->integer('question_id')->index()->unsigned();
       $table->text('content');
       $table->integer('votes_count')->default(0);
       $table->integer('comments_count')->default(0);
       $table->string('is_hidden',8)->default('F');
       $table->string('close_comment',8)->default('F');
       $table->timestamps();
   });
}

执行数据迁移

php artisan migrate

修改 app/Answer.php

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

public function user()
{
   return $this->belongsTo(User::class);
}

public function question()
{
   return $this->belongsTo(Question::class);
}

修改 app/Question.php

public function answers()
{
   return $this->hasMany(Answer::class);
}

修改 app/User.php

public function answers()
{
   return $this->hasMany(Answer::class);
}

修改 app/Repostories/QuestionRepostry.hp

public function byIdWithTopicsAndAnswers($id)
{
   return Question::where('id', $id)->with(['topics','answers'])->first();
}

修改 app/Http/Controllers/QuestionsController.php

public function show($id)
{
   $question = $this->questionRepository->byIdWithTopicsAndAnswers($id);
   return view('questions.show', compact('question'));
}

二、回答流程

创建回答控制器

php artisan make:controller AnswersController

注册路由:修改 routes/web.php

Route::post('questions/{question}/answer','AnswersController@store');

创建验证器

php artisan make:request StoreAnswerRequest

修改 app/Http/Requests/StoreAnswerRequest.php

public function authorize()
{
   return true;
}

public function messages()
{
   return [
       'content.required' => '内容不能为空',
       'content.min' => '回答至少10个字符',
   ];
}

public function rules()
{
   return [
       'content' => 'required|min:10'
   ];
}

新建 app/Repostories/AnswerRepostry.hp

namespace App\Repositories;

use App\Answer;

class AnswerRepository
{
   public function create(array $attributes)
   {
       return Answer::create($attributes);
   }
}

修改 app/Http/Controllers/AnswersController.php

use App\Http\Requests\StoreAnswerRequest;
use App\Repositories\AnswerRepository;
use Auth;

protected $answerRepository;

public function __construct(AnswerRepository $answerRepository)
{
   $this->answerRepository = $answerRepository;
}

public function store(StoreAnswerRequest $request, $question)
{
   $answer = $this->answerRepository->create([
       'question_id' => $question,
       'user_id' => Auth::id(),
       'content' => $request->get('content')
   ]);
   $answer->question()->increment('answers_count');
   return back();
}

修改 resources/views/questions/show.blade.php

https://file.lulublog.cn/images/3/2023/07/nWWpAWrUFprPSxGSl55xAT54ZXwr5w.png

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

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

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 259