Skip to content

Instantly share code, notes, and snippets.

@giordanolima
Created March 19, 2019 18:37
Show Gist options
  • Save giordanolima/20e434d0cbfe93711a406396148e0497 to your computer and use it in GitHub Desktop.
Save giordanolima/20e434d0cbfe93711a406396148e0497 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Database\Seeder;
class BlogSeeder extends Seeder {
public function run() {
$faker = Faker\Factory::create('pt_BR');
$categories = App\Models\Blog\Categories::query()->pluck("id");
foreach($categories as $cat) {
factory(App\Models\Blog\Posts::class, $faker->numberBetween(5, 10))->create([
"category_id" => $cat
]);
}
$posts = App\Models\Blog\Posts::all();
$posts->each(function ($post) use($faker) {
$end = $faker->numberBetween(0,4);
for($i = 0; $i <= $end; $i++) {
// Dentro da pasta storage/app/temp eu tenho 10 arquivos pra usar como imagens das postagens
$arquivo = $faker->numberBetween(1,10) .".jpg";
$path = storage_path("app/temp/". $arquivo);
// Copia a imagem pro lugar que tu quiser em $destination
// Se quiser ainda pode ser feito outras operações,
// como tratamentos de banco de dados e etc..
// Ou então simplesmente pode chamar algum service
copy($path, $destination);
}
factory(App\Models\Blog\Comments::class, $faker->numberBetween(10, 50))->create([
"post_id" => $post->id
]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment