/** * Store a newly created resource in storage. * * @param $categoryId * @param $threadId * @param PostRequest $request * @return Response */ public function store($categoryId, $threadId, PostRequest $request) { if (!Sentinel::getUser()->hasAccess(['posts.create'])) { abort(401); } $thread = Thread::with(['category' => function ($query) use($categoryId) { $query->where('id', $categoryId); }])->findOrFail($threadId); $post = new Post($request->all()); $post->user_id = Sentinel::getUser()->id; $post->thread_id = $thread->id; $post->save(); return redirect()->route('categories.threads.posts.index', [$thread->category->id, $thread->id]); }
/** * Remove the specified resource from storage. * * @param $categoryId * @param $threadId * @return Response */ public function destroy($categoryId, $threadId) { $thread = Thread::with(['category' => function ($query) use($categoryId) { $query->where('id', $categoryId); }, 'posts' => function ($query) { $query->orderBy('created_at', 'asc'); }])->findOrFail($threadId); if (Sentinel::getUser()->id == $thread->posts->first()->user->id && !Sentinel::getUser()->hasAccess(['threads.own.destroy']) || Sentinel::getUser()->id != $thread->posts->first()->user->id && !Sentinel::getUser()->hasAccess(['threads.others.destroy'])) { abort(401); } $thread->posts()->delete(); $thread->delete(); return redirect()->route('categories.threads.index', [$thread->category->id]); }