/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { // $data = $request->all(); $rules = ['body' => 'required', 'tag_id' => 'required|numeric', 'tag' => 'required|min:3,max:254', 'access' => 'required|min:1,max:254', 'type' => 'required|min:3,max:254']; if ($data['access'] == 1) { $rules['name'] = 'required|min:3,max:254'; $rules['email'] = 'required|email'; } else { $rules['title'] = 'required|min:3,max:254'; } $validator = \Validator::make($data, $rules); $json = array('status' => true, 'data' => null); if ($validator->fails()) { $json['status'] = false; $json['data'] = $validator->errors()->all(); } if ($json['status']) { $comment = new \App\Comment($data); $comment->save(); } if ($request->ajax()) { return response()->json($json); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $comment = new \App\Comment(); $comment->user_id = \Auth::user()->id; $comment->comment_id = $request->comment_id; $comment->post_id = $request->post_id; $comment->content = $request->content; $comment->save(); return $comment; }
public function addComment(CommentRequest $request, $id) { if (\Auth::check()) { $user = \Auth::user(); $post = \App\Post::findOrFail($id); $comment = new \App\Comment($request->all()); $comment->user()->associate($user); $comment->post()->associate($post); $comment->save(); return redirect('/post/' . $post->id); } else { return redirect('/auth/login'); } }
public function testFieldOutput() { $field = new RowSetType('name', 'rowset', $this->plainForm, ['model' => 'App\\Comment', 'data' => App\Comment::all()]); $res = $field->render(); self::assertCount(9, $field->getOption('children')); self::assertEquals(9, substr_count($res, 'createRowSetRow')); }
/** * Run the database seeds. * * @return void */ public function run() { App\Comment::truncate(); $users = App\User::all(); factory(App\Comment::class, 20)->create()->each(function ($comment) use($users) { $comment->author()->associate($users->random())->save(); }); }
public function run() { DB::table('comments')->truncate(); $faker = Faker\Factory::create('zh_TW'); foreach (range(1, 10) as $number) { App\Comment::create(['name' => $faker->name, 'email' => $faker->email, 'content' => $faker->paragraph, 'post_id' => rand(1, 10), 'created_at' => Carbon\Carbon::now()->addDays($number), 'updated_at' => Carbon\Carbon::now()->addDays($number)]); } }
public function run() { for ($i = 0; $i < 4; $i++) { App\Comment::create(['body' => 'This is comment ' . $i, 'article_id' => 4]); } for ($i = 4; $i < 6; $i++) { App\Comment::create(['body' => 'This is comment ' . $i, 'article_id' => 5]); } }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\User::class, 50)->create()->each(function ($user) { $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
function stampa($komentari, $a) { if ($komentari) { foreach ($komentari as $komentar) { array_push($a, $komentar); $sledeci = App\Comment::where('parent_id', '=', $komentar->id)->where('article_id', '=', $komentar->article_id)->get(); $a = stampa($sledeci, $a); } } return $a; }
/** * Run the database seeds. * * @return void */ public function run() { // $this->call(UserTableSeeder::class); // change # to 10 for now factory(App\User::class, 10)->create()->each(function ($user) { $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
/** * Run the database seeds. * * @return void */ public function run() { $bookings = \App\Booking::where('status', 'confirmed')->get(); $fake_comments = file_get_contents("http://jsonplaceholder.typicode.com/comments"); $fake_comments = json_decode($fake_comments, true); foreach ($bookings as $booking) { if (mt_rand(0, 1)) { $comments = \App\Comment::where('booking_id', $booking->id)->get(); if (count($comments) > 0) { continue; } else { $comment = new \App\Comment(); $comment->user_id = $booking->customer_id; $comment->property_id = $booking->property_id; $comment->booking_id = $booking->id; $comment->rating = mt_rand(1, 5); $one_comment = $fake_comments[mt_rand(0, count($fake_comments) - 1)]; $comment->comment_text = $one_comment['body']; $comment->written_at = date('Y-m-d H:i:s'); $comment->save(); } } } }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\User::class, 50)->create()->each(function ($user) { // Each user will make 1 subbreddit $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); // Each user will make 1 post on a random subbreddit $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); // Each user will leave a comment on a random post $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); // Each user will leave a comment on a random comment $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); // Each user will subscribe to 1 subbreddit $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\Project::class, 20)->create()->each(function ($project) { $max = rand(5, 10); for ($i = 0; $i < $max; $i++) { $project->goal()->save(factory(App\Goal::class)->make()); } $max = rand(5, 10); for ($i = 0; $i < $max; $i++) { $project->reward()->save(factory(App\Reward::class)->make()); } $max = rand(5, 20); for ($i = 0; $i < $max; $i++) { $project->payment()->save(factory(App\Payment::class)->make()); } $max = rand(5, 20); for ($i = 0; $i < $max; $i++) { $project->comment()->save(factory(App\Comment::class)->make(['type' => 1])); } $max = rand(5, 20); for ($i = 0; $i < $max; $i++) { $project->updates()->save(factory(App\Content::class)->make(['type' => 3])); } }); $payments = App\Payment::where('value', 0)->get(); foreach ($payments as $p) { $project = App\Project::find($p->project_id); $reward = $project->reward()->orderBy(DB::raw('RAND()'))->take(1)->first(); $p->value = $reward->value; $p->reward_id = $reward->id; $p->save(); } $comments = App\Comment::where('type', 1)->where('reply_id', 1)->get(); foreach ($comments as $c) { $parents = App\Comment::where('type', 1)->where('item_id', $c->item_id)->orderByRaw("RAND()")->first(); if ($parents) { $c->reply_id = $parents->id; $c->save(); } } }
var_dump($faker->firstName . ' ' . $faker->lastName); } }); /*********************************************************************************/ Route::post('validation', function () { $rules = array('name' => 'required', 'link' => 'required|url', 'password' => 'required|min:8', 'password-repeat' => 'required|same:password'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::to('home')->withInput()->withErrors($validator->messages()); } return 'Form was submitted'; }); /***********************************************************************************/ Route::get('comment', function () { // Grabbing the article of a particular comment $article = App\Comment::find(7)->article; dd($article->title); // Grabbing the comments for a particular article $comments = App\Article::find(4)->comments; //->where('body', '>', 50)->get(); foreach ($comments as $comment) { var_dump($comment->body); } }); Route::get('article2', function () { /* DELETING ARTICLES */ // deleting multiple articles $victims = App\Article::where('id', '<', 3); $victims->delete();
/** * Add a Comment * */ public function postComment() { $data = \Input::all(); // Check Post exists if (empty($data['post_id'])) { return response()->json(['status' => 404, 'error' => "Post not Found"], 404); } $post = \App\Post::find($data['post_id']); if (!$post) { return response()->json(['status' => 404, 'error' => "Post not Found"], 404); } // Build Comment record $comment = new \App\Comment(['post_id' => $post->id, 'user_id' => \Auth::user()->id, 'body' => $data['comment_body']]); if ($comment->save()) { return response()->json(['status' => 200], 200); } else { return response()->json(['status' => 500, 'error' => "Saved Failed"], 500); } }
$firs_name = $faker->firstName; $last_name = $faker->lastName; return ['identification' => $faker->unique()->numberBetween(5000000, 39999999), 'first_name' => $firs_name, 'last_name' => $last_name, 'avatar' => 'vendor/AdminTheme/images/avatar/default.png', 'type' => $faker->randomElement(['teacher', 'student']), 'email' => $faker->unique()->email, 'password' => '123456']; }); $factory->define(App\Course::class, function ($faker) { return ['title' => $faker->sentence(3), 'description' => $faker->paragraph(10), 'image' => $faker->imageUrl(660, 260), 'user_id' => App\User::all()->random()->id]; }); $factory->define(App\Unit::class, function ($faker) { return ['pos' => $faker->randomNumber(1), 'title' => $faker->sentence(5), 'description' => $faker->paragraph(10), 'image' => $faker->imageUrl(660, 260), 'user_id' => App\User::all()->random()->id, 'course_id' => App\Course::all()->random()->id]; }); $factory->define(App\Lesson::class, function ($faker) { return ['pos' => $faker->randomNumber(1), 'title' => $faker->sentence(5), 'content' => $faker->paragraph(10), 'user_id' => App\User::all()->random()->id, 'unit_id' => App\Unit::all()->random()->id]; }); $factory->define(App\Question::class, function ($faker) { return ['title' => $faker->sentence(2) . '?', 'unit_id' => App\Unit::all()->random()->id, 'user_id' => App\User::all()->random()->id]; }); $factory->define(App\Answer::class, function ($faker) { return ['content' => $faker->sentence(5), 'correct' => rand(0, 1), 'question_id' => App\Question::all()->random()->id, 'user_id' => App\User::all()->random()->id]; }); $factory->define(App\Comment::class, function ($faker) { return ['content' => $faker->paragraph(5), 'user_id' => App\User::all()->random()->id, 'lesson_id' => App\Lesson::all()->random()->id]; }); $factory->define(App\Reply::class, function ($faker) { return ['content' => $faker->paragraph(5), 'user_id' => App\User::all()->random()->id, 'comment_id' => App\Comment::all()->random()->id]; }); $factory->define(App\Evaluation::class, function ($faker) { return ['value' => $faker->numberBetween(15, 20), 'min_value' => $faker->numberBetween(50, 85), 'max_question' => $faker->numberBetween(5, 6), 'unit_id' => App\Unit::all()->random()->id, 'user_id' => App\User::all()->random()->id]; }); $factory->define(App\CourseUser::class, function ($faker) { return ['course_id' => App\Course::all()->random()->id, 'user_id' => App\User::all()->random()->id]; });
/** * Run the database seeds. * * @return void */ public function run() { /* * Prepare seeding */ $faker = Faker::create(); if (config('database.default') != 'sqlite') { DB::statement('SET FOREIGN_KEY_CHECKS=0'); } Model::unguard(); /* * Seeding users table */ App\User::truncate(); factory(App\User::class)->create(['name' => 'John Doe', 'email' => '*****@*****.**', 'password' => bcrypt('password')]); factory(App\User::class, 9)->create(); $this->command->info('users table seeded'); /** * Seeding roles table */ Bican\Roles\Models\Role::truncate(); DB::table('role_user')->truncate(); $adminRole = Bican\Roles\Models\Role::create(['name' => 'Admin', 'slug' => 'admin']); $memberRole = Bican\Roles\Models\Role::create(['name' => 'Member', 'slug' => 'member']); App\User::where('email', '!=', '*****@*****.**')->get()->map(function ($user) use($memberRole) { $user->attachRole($memberRole); }); App\User::whereEmail('*****@*****.**')->get()->map(function ($user) use($adminRole) { $user->attachRole($adminRole); }); $this->command->info('roles table seeded'); /* * Seeding articles table */ App\Article::truncate(); $users = App\User::all(); $users->each(function ($user) use($faker) { $user->articles()->save(factory(App\Article::class)->make()); $user->articles()->save(factory(App\Article::class)->make()); }); $this->command->info('articles table seeded'); /** * Seeding comments table */ App\Comment::truncate(); $articles = App\Article::all(); $articles->each(function ($article) use($faker, $users) { $article->comments()->save(factory(App\Comment::class)->make(['author_id' => $faker->randomElement($users->lists('id')->toArray())])); }); $this->command->info('comments table seeded'); /* * Seeding tags table */ App\Tag::truncate(); DB::table('article_tag')->truncate(); $rawTags = ['General', 'Laravel', 'Lumen', 'Eloquent', 'Servers', 'Tips', 'Lesson Feedback']; foreach ($rawTags as $tag) { App\Tag::create(['name' => $tag, 'slug' => str_slug($tag)]); } $tags = App\Tag::all(); foreach ($articles as $article) { $article->tags()->attach($faker->randomElements($tags->lists('id')->toArray(), $faker->randomElement([1, 2, 3]))); } $this->command->info('tags table seeded'); /* * Seeding attachments table */ App\Attachment::truncate(); if (!File::isDirectory(attachment_path())) { File::deleteDirectory(attachment_path(), true); } $articles->each(function ($article) use($faker) { $article->attachments()->save(factory(App\Attachment::class)->make()); }); $files = App\Attachment::lists('name'); if (!File::isDirectory(attachment_path())) { File::makeDirectory(attachment_path(), 777, true); } foreach ($files as $file) { File::put(attachment_path($file), ''); } $this->command->info('attachments table seeded'); /** * Close seeding */ Model::reguard(); if (config('database.default') != 'sqlite') { DB::statement('SET FOREIGN_KEY_CHECKS=1'); } }
public function testDeleteWithRelations() { // one-to-many, many-to-many App\Post::find(1)->deleteWithRelations(); self::assertEquals(App\Post::where('id', 1)->count(), 0); self::assertEquals(App\Comment::where('post_id', 1)->count(), 0); self::assertEquals(DB::table('lv_post_tag')->where('post_id', 1)->count(), 0); // one-to-one App\Post::getDeleteDescriptor()->removeDescription('comments'); App\Comment::getDeleteDescriptor()->add('post'); App\Comment::find(4)->deleteWithRelations(); self::assertEquals(App\Comment::where('id', 4)->count(), 0); self::assertEquals(App\Post::where('id', 2)->count(), 0); // db rollback $this->rollback(); }
/** * Store a newly created comment. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $comment = new \App\Comment(); $comment->create(['user_id' => \Auth::user()->id, 'comment' => $request->comment, 'article_id' => $request['article_id']]); return redirect()->back(); }
public function comment(Request $request, $id) { $book = \App\Book::findOrFail($id); // $user = \App\User::($id); $commented = $request->input('comment'); $user = Auth::user(); $comment = new \App\Comment(); $comment->comment = $commented; $comment->save(); $book->comments()->attach($comment); $user->comments()->attach($comment); return Redirect::back(); }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); $tag_ids = []; $users = []; $nodes = []; static::nodeRecurse($nodes); for ($a = 1; $a <= 5; $a++) { $tag = new \App\Tag(); $faker->seed($a + 10); $tag->name = $faker->word; $tag->save(); $tag_ids[] = $tag->id; } $faker->unique(true); for ($b = 1; $b <= 20; $b++) { $user = new \App\User(); $faker->seed($a); $user->name = $faker->unique()->userName; $user->email = $faker->unique()->email; $user->password = $faker->password; $user->save(); $users[] = $user; } foreach ($users as $user) { $faker->seed($user->id); if ($faker->boolean(40)) { // Author $author = new \App\Author(); $author->user()->associate($user); $author->name = $faker->name; $author->save(); if ($faker->boolean(50)) { $number_of_likes = $faker->numberBetween(1, count($users)); $faker->unique(true); for ($c = 1; $c <= $number_of_likes; $c++) { $like = new \App\Like(); $user_key = $faker->unique()->randomElement(array_keys($users)); $like->user()->associate($users[$user_key]); $like->likeable()->associate($author); $like->save(); } } if ($faker->boolean(20)) { $number_of_reports = $faker->numberBetween(1, round(count($users) / 3)); $faker->unique(true); for ($d = 1; $d <= $number_of_reports; $d++) { $report = new \App\Report(); $user_key = $faker->unique()->randomElement(array_keys($users)); $report->user()->associate($users[$user_key]); $report->save(); $report->authors()->attach($author); } } if ($faker->boolean(75)) { // At leat 1 article $article_count = $faker->numberBetween(1, 15); for ($e = 1; $e < $article_count; $e++) { $article = new \App\Article(); $article->author()->associate($author); $article->title = $faker->sentence($faker->numberBetween(3, 10)); $article->body = $faker->paragraphs($faker->numberBetween(2, 8), true); $article->node()->associate($faker->randomElement($nodes)); $article->save(); $article->tags()->sync($faker->randomElements($tag_ids, $faker->numberBetween(0, min(3, count($tag_ids))))); if ($faker->boolean(10)) { $number_of_reports = $faker->numberBetween(1, round(count($users) / 3)); $faker->unique(true); for ($f = 1; $f <= $number_of_reports; $f++) { $report = new \App\Report(); $user_key = $faker->unique()->randomElement(array_keys($users)); $report->user()->associate($users[$user_key]); $report->save(); $report->articles()->attach($article); } } if ($faker->boolean(90)) { $review = new \App\Review(); $user_key = $faker->randomElement(array_keys($users)); $review->user()->associate($users[$user_key]); $review->reviewable()->associate($article); $review->save(); } if ($faker->boolean(69)) { $number_of_likes = $faker->numberBetween(1, count($users)); $faker->unique(true); for ($g = 1; $g <= $number_of_likes; $g++) { $like = new \App\Like(); $user_key = $faker->unique()->randomElement(array_keys($users)); $like->user()->associate($users[$user_key]); $like->likeable()->associate($article); $like->save(); } } } if ($faker->boolean(70)) { // At leat 1 comment $comment_count = $faker->numberBetween(1, 20); for ($h = 1; $h < $comment_count; $h++) { $comment = new \App\Comment(); $comment->article()->associate($article); $comment->user()->associate($user); $comment->body = $faker->paragraph(4, true); $comment->save(); if ($faker->boolean(15)) { $number_of_reports = $faker->numberBetween(1, round(count($users) / 3)); $faker->unique(true); for ($i = 1; $i <= $number_of_reports; $i++) { $report = new \App\Report(); $user_key = $faker->unique()->randomElement(array_keys($users)); $report->user()->associate($users[$user_key]); $report->save(); $report->comments()->attach($comment); } } if ($faker->boolean(90)) { $review = new \App\Review(); $user_key = $faker->randomElement(array_keys($users)); $review->user()->associate($users[$user_key]); $review->reviewable()->associate($comment); $review->save(); } if ($faker->boolean(60)) { $number_of_likes = $faker->numberBetween(1, count($users)); $faker->unique(true); for ($j = 1; $j <= $number_of_likes; $j++) { $like = new \App\Like(); $user_key = $faker->unique()->randomElement(array_keys($users)); $like->user()->associate($users[$user_key]); $like->likeable()->associate($comment); $like->save(); } } } } } } } }