一、问题列表
app/Question.php 新增方法
public function user()
{
return $this->belongsTo(User::class);
}
public function scopePublished($query)
{
return $query->where('is_hidden','F');
}
app/Repositories/QuestionRepository.php 新增方法
public function getQuestionsFeed()
{
return Question::published()->latest('updated_at')->with('user')->get();
}
修改 app/Providers/AppServiceProvider.php 的方法
public function register()
{
if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
}
修改 app/Http/Controllers/QuestionsController.php
public function index()
{
$questions = $this->questionRepository->getQuestionsFeed();
return view('questions.index', compact('questions'));
}
修改 routes/web.php
Route::get('/', 'QuestionsController@index');
修改 app/Http/Controllers/Auth/LoginController.php
protected $redirectTo = '/';
修改 app/Http/Middlerware/RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
return $next($request);
}
新增 resources/views/questions/index.blade.php
二、删除问题
修改 app/Http/Controllers/QuestionsController.php
public function destroy($id)
{
$question = $this->questionRepository->byId($id);
if(Auth::user()->owns($question)){
$question->delete();
return redirect('/');
}
abort(403,'Forbidden');
}
修改 resources/views/questions/show.blade.php