<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
use Carbon\Carbon;
use App\Http\Requests\CreateArticleRequest; //一定要写,否则会和namespace混淆path
class ArticlesController extends Controller
{
//return artilces
public function index(){
$articles = Article::latest()->published()->get(); //scopePublished的使用
return view('articles.index')->with('articles', $articles);
}
public function show($id){
$article=Article::findOrFail($id);
return view('articles.show')->with('article', $article);
}
public function create(){
return view('articles.create');
}
public function store(CreateArticleRequest $request){
$input=$request->all();
Article::create($input); // create自动过滤掉token
return redirect('/articles');
}
public function edit($id){
$article=Article::findOrFail($id);
return view('articles.edit')->with('article', $article);
}
public function update(CreateArticleRequest $request){ //实现表单验证
$article=Article::findOrFail($id);
$article->update($request->all());
return redirect('/articles');
}
}