public function store(Request $request) { $category = Category::findOrFail($request->parent_id); $this->authorize('laraboard::board-create', $category); $this->validate($request, ['name' => 'required|max:255', 'body' => 'max:255']); $board = new Post(); $board->name = $request->name; $board->body = $request->body; $board->type = 'Board'; $board->user_id = \Auth::user()->id; $board->save(); $board->makeChildOf($category); return redirect()->route('board.show', [$board->slug, $board->name_slug])->with('success', 'Board created successfully.'); }
public static function boot() { parent::boot(); static::addGlobalScope('forumThread', function (Builder $builder) { $builder->where('type', 'Thread'); }); }
/** * put your comment there... * * @param Request $request * @return {\Illuminate\Http\RedirectResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\RedirectResponse} */ public function store(Request $request) { $this->authorize('laraboard::category-create'); $this->validate($request, ['name' => 'required|max:255', 'body' => 'max:255']); // category names must be unique if (Category::whereName(strip_tags($request->name))->count() > 0) { return redirect()->back()->withInput()->with('danger', 'Category names must be unique.'); } $category = new Post(); $category->name = $request->name; $category->body = $request->body; $category->type = 'Category'; $category->user_id = \Auth::user()->id; $category->save(); return redirect()->route('forum.index')->with('success', 'Category created successfully.'); }
public function reply($slug) { $thread = Thread::whereSlug($slug)->firstOrFail(); $this->authorize('laraboard::thread-reply', $thread); event(new \Christhompsontldr\Laraboard\Events\ThreadViewed($thread, \Auth::user())); $posts = Post::where('id', $thread->id)->first()->getDescendantsAndSelf()->reverse()->slice(0, 100); return view('laraboard::thread.reply', compact('thread', 'posts')); }
public function search(Request $request, $term = null) { if (!$term && $request->has('term')) { return redirect()->route('forum.search', $request->input('term')); } $posts = Post::search($term)->paginate(config('laraboard.post.limit', 15)); return view('laraboard::forum.search', compact('posts', 'term')); }
/** * Register any events for your application. * * @return void */ public function boot() { parent::boot(); // loose wiring of a Laravel observer Post::saving(function ($post) { Event::fire(new \Christhompsontldr\Laraboard\Events\PostSaving($post)); }); // loose wiring of a Laravel observer Post::created(function ($post) { Event::fire(new \Christhompsontldr\Laraboard\Events\PostCreated($post)); }); }
public function delete($id) { $post = Post::findOrFail($id); $thread = $post->thread; $this->authorize('laraboard::post-delete', $post); $post->status = 'Deleted'; $post->save(); $post->delete(); if ($post->type == 'Thread') { return redirect()->route('forum.index')->with('success', 'Reply deleted.'); } return redirect()->route('thread.show', $thread->lastPageRoute)->with('success', 'Reply deleted.'); }
/** * Adds a slug to the post if it's not already set * * @param PostSaving $event * @return void */ public function handle(PostSaving $event) { // give a slug field to any post that doesn't have one if (empty($event->post->slug)) { // categories get slugs from their title if ($event->post->type == 'Category' || $event->post->type == 'Board') { $event->post->slug = str_slug(trim(str_limit($event->post->name, config('laraboard.category.slug_limit', 50)))); } else { /** * @todo this could be moved to a scheduled task, possibly create a cache key that contains 100 new slugs */ $found = 0; while ($found < 1) { $slug = strtolower(str_random(6)); $found = Post::whereSlug($slug)->count(); if ($found == 0) { $event->post->slug = $slug; $found = 1; } } } } }