/** * Run the database seeds. * * @return void */ public function run() { for ($count = 1; $count <= 10; $count++) { $thread_id = rand(1, DB::table('threads')->count()); $position = Helper::appendZero($count, 5); $user_id = rand(1, DB::table('users')->count()); DB::table('replies')->insert(['thread_id' => $thread_id, 'user_id' => $user_id, 'content' => str_random(100), 'position' => $position . ',', 'depth' => 0]); DB::table('threads')->where('id', $thread_id)->increment('comment_count'); DB::table('users')->where('id', $user_id)->increment('comment_count'); $author_id = DB::table('threads')->where('id', $thread_id)->first()->author_id; DB::table('notifications')->insert(['type' => 1, 'user_id' => $author_id, 'content_id' => $count]); } for ($count = 11; $count <= 30; $count++) { $parent_id = rand(1, DB::table('replies')->count()); $parent = DB::table('replies')->where('id', $parent_id)->first(); $position = $parent->position . Helper::appendZero($count, 5) . ','; $thread_id = $parent->thread_id; $user_id = rand(1, DB::table('users')->count()); DB::table('replies')->insert(['thread_id' => $thread_id, 'user_id' => $user_id, 'content' => str_random(100), 'parent_id' => $parent_id, 'depth' => $parent->depth + 1, 'position' => $position]); DB::table('threads')->where('id', $thread_id)->increment('comment_count'); DB::table('users')->where('id', $user_id)->increment('comment_count'); $author_id = DB::table('threads')->where('id', $thread_id)->first()->author_id; DB::table('notifications')->insert(['type' => 1, 'user_id' => $author_id, 'content_id' => $thread_id]); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @param int $thread_id * @param int $parent_id * @return \Illuminate\Http\Response */ public function store(Request $request, $thread_id, $parent_id = null) { $user_id = Auth::user()->id; $position = ''; $depth = 0; $thread = DB::table('threads')->where('id', $thread_id)->first(); if ($thread == null) { abort(404); } if ($parent_id != null) { $parent = DB::table('replies')->where('id', $parent_id)->first(); if ($parent == null || $parent->thread_id != $thread_id) { abort(404); } $position = $parent->position; $depth = $parent->depth + 1; } // Validation $this->validate($request, ['content' => 'string|required']); $content = $request->input('content'); $id = DB::table('replies')->insertGetId(['thread_id' => $thread_id, 'user_id' => $user_id, 'parent_id' => $parent_id, 'content' => $content, 'depth' => $depth]); $position = $position . Helper::appendZero($id, 5) . ','; DB::table('replies')->where('id', $id)->update(['position' => $position]); DB::table('threads')->where('id', $thread_id)->increment('comment_count'); DB::table('users')->where('id', $user_id)->increment('comment_count'); $author_id = DB::table('threads')->where('id', $thread_id)->first()->author_id; DB::table('notifications')->insert(['type' => 1, 'user_id' => $author_id, 'content_id' => $thread_id]); return redirect('/thread/' . $thread_id); }