public function run() { $users = MyUser::all(); $ids = $users->lists('id'); $faker = Factory::create(); foreach (MyPost::all() as $post) { if (!empty($ids)) { $postuser = new MyPostUser(); $postuser->user_id = $ids[$faker->numberBetween(0, count($ids) - 1)]; $postuser->post_id = $post->id; $postuser->save(); } } }
public function run() { $ids = MyUser::all()->lists('id'); $faker = Factory::create(); for ($i = 1; $i <= 10; $i++) { $post = new MyPost(); $post->title = $faker->sentence(8); // We'll try to extract the excerpt from the body so let's do // some magic. $body = $faker->text(); $post->body = $body; $authorId = 0; if (!empty($ids)) { // Just additional protection $authorId = $ids[$faker->numberBetween(0, count($ids) - 1)]; } $post->author_id = $authorId; $parts = explode(' ', $body); $excerptStart = rand(0, count($parts) - 7); $post->excerpt = implode(' ', array_slice($parts, $excerptStart, 6)); $post->save(); } }
public function run() { $tags = MyTag::all(); $ids = $tags->lists('id'); $total = count($ids); $faker = Factory::create(); foreach (MyPost::all() as $post) { if (!empty($ids)) { // Generate random number of tags foreach (range(1, $faker->numberBetween(0, $total - 1)) as $value) { $postTag = new MyPostTag(); $postTag->tag_id = $ids[$faker->numberBetween(0, $total - 1)]; $postTag->post_id = $post->id; try { $postTag->save(); } catch (\Exception $e) { // Skip } } } } }
/** * * @param type $id * @return \Illuminate\Database\Eloquent\Model BlogPost repost */ public function getBlog($id) { return MyPost::findOrFail($id); }