My opinion is that include a user_id column in the article database, and then you may use relation to get the article user_id and check whether it is Auth::user()->id before allowing someone using the edit/update button.
Jul 19, 2016
6
Level 2
Check Current Users Permission to update article
Hey, I am trying to master laravel fundamentals https://laracasts.com/series/laravel-5-fundamentals/episodes/14 ans stuck on something I can't figure out.
Can somebody point me into the right direction how to check if the current user is the owner of an article an has the permission to edit/update the article?
My ArticlesController is: class ArticlesController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function index() {
$articles = Article::latest('published_at')->published()->get();
return view('articles.index')->withArticles($articles);
}
public function show($id) {
$article = Article::findOrFail($id);
return view('articles.show')->withArticle($article);
}
public function create() {
return view('articles.create');
}
public function store(ArticleRequest $request) {
$article = new Article($request->all());
Auth::user()->articles()->save($article);
return redirect('articles');
}
public function edit($id) {
$article = Article::findOrFail($id);
return view('articles.edit')->withArticle($article);
}
public function update($id, ArticleRequest $request) {
$article = Article::findOrFail($id);
$article->update($request->all());
return redirect('articles');
}
}
and my ArticleRequest is: class ArticleRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; }
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'body' => 'required',
'published_at' => 'required|date'
];
}
}
Thanks in advance
Please or to participate in this conversation.