option('user_id'); if($user_id) { $user = User::find($user_id); if(!$user) { throw new \Exception("User $user_id doesn't exist"); } } else { $user = User::inRandomOrder()->first(); if(!$user) { throw new \Exception("User $user_id doesn't exist"); } } $amount = $this->argument('amount'); if(!$amount) { $amount = 1; } $postType = $this->argument('postType'); $postTypes = config('postTypes'); if(array_key_exists($postType, $postTypes)) { /** @var Post $model */ $postRepo = new PostRepository($postTypes[$postType]['model']); $faker = \Faker\Factory::create(); for($i = 0; $i < $amount; $i++) { $post = $postRepo->createModel(); $post->fill([ 'title' => $this->randomTextLength($faker, 10, 25), 'excerpt' => $this->randomTextLength($faker, 10, 30), 'introduction' => $faker->realText($faker->numberBetween(20, 400)), 'user_id' => $user->id ]); $post->save(); } } } private function randomTextLength($faker, $min, $max) { return mb_substr($this->rtrim($faker->realText($max)), 0, $faker->numberBetween($min, $max - 1)); } private function rtrim($text) { return mb_substr($text, 0, -1); } }