/** * Run the database seeds. * * @return void */ public function run() { //factory(PostHasTag::class, 50)->create(); $posts = App\Post::all(); $tags = App\Tag::all(); foreach ($posts as $post) { $list = []; foreach ($tags as $tag) { rand(0, 5) == 0 && count($list) < 10 ? $list[] = $tag->id : null; } $post->tags()->sync($list); } }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\Product::class, 15)->create()->each(function ($product) { $allTags = App\Tag::all(); $tags = $allTags; $nbProductTags = rand(1, $allTags->count()); while ($nbProductTags != 0) { $indiceTags = rand(1, $tags->count()) - 1; DB::table('product_tag')->insert([['product_id' => $product->id, 'tag_id' => $tags[$indiceTags]->id]]); // $Product->tags()->attach([$tags[$indiceTags]->id]); // voir la solution du prof $tags->splice($indiceTags, 1); //suppression de l'élément dans la collection $tags $nbProductTags--; } }); }
/** * Run the migrations. * * @return void */ public function up() { Schema::create('lv_posts', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); $table->string('title'); $table->text('body'); }); $tags = App\Tag::all(); factory(App\Post::class, 3)->create()->each(function ($p) use($tags) { for ($i = 0; $i < 3; $i++) { $p->comments()->save(factory(App\Comment::class)->make()); } foreach ($tags as $t) { $p->tags()->attach($t['id']); } }); }
/** * Run the database seeds. * * @return void */ public function run() { // Generate users factory(App\User::class, 10)->create()->each(function (App\User $user) { $user->profileImage()->associate(factory(App\Image::class, 1)->make()); }); // Generate tags factory(App\Tag::class, 8)->create(); // Generate projects factory(App\Project::class, 16)->create()->each(function (App\Project $project) { // Associate with contributors and tags $project->tags()->saveMany(App\Tag::all()->random(random_int(2, 6))); $project->contributors()->saveMany(App\User::all()->random(2)); // Add content $project->colors()->saveMany(factory(App\Color::class, random_int(2, 5))->make()); $project->fonts()->saveMany(factory(App\Font::class, random_int(2, 3))->make()); $project->images()->saveMany(factory(App\Image::class, random_int(3, 5))->make()); $project->textures()->saveMany(factory(App\Texture::class, random_int(3, 5))->make()); }); // Generate comments factory(App\Comment::class, 20)->create(); // Generate notifications factory(App\Notification::class, 20)->create(); }
/** * 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 testSaveWithRelations() { // one-to-many, many-to-many $item = factory(App\Post::class)->make(); $post = $item->toArray(); $post['comments'] = []; $post['comments'][] = factory(App\Comment::class)->make()->toArray(); $post['tags'] = []; foreach (App\Tag::all() as $t) { $post['tags'][] = $t['id']; } $item->saveWithRelations($post); $item = App\Post::find(4); self::assertInstanceOf('App\\Post', $item); self::assertEquals('4', $item['id']); $tags = $item->tags; self::assertCount(3, $tags); $comments = $item->comments; self::assertCount(1, $comments); // one-to-one $item = factory(App\Comment::class)->make(); $comment = $item->toArray(); $comment['post'] = factory(App\Post::class)->make()->toArray(); $item->saveWithRelations($comment); $item = App\Comment::find(11); self::assertEquals('11', $item['id']); self::assertEquals(11, App\Comment::count()); $post = $item->post; self::assertEquals('5', $post['id']); self::assertEquals(5, App\Post::count()); // db rollback $this->rollback(); }
return redirect()->route('transparansi')->withTag("tag")->withTop("top"); }); Route::get('/ratio/top', function () { return redirect()->route('transparansi')->withTag("ratio")->withTop("top"); }); Route::get('/transparansi', ['as' => 'transparansi', 'uses' => 'DataController@index']); Route::get('/transparansi/{tipeDana?}', function ($tipeDana = "belanja") { return redirect()->route('transparansi')->withDana($tipeDana); }); Route::get('/transparansi/{tipeDana?}/tipe/{type?}', function ($tipeDana = "belanja", $type = "area") { return redirect()->route('transparansi')->withDana($tipeDana)->withType($type); }); Route::get('/dinas/{tipeDana?}/tipe/{type?}/id/{id?}', function ($tipeDana = "belanja", $type = "area", $id = null) { return redirect()->route('transparansi')->withDana($tipeDana)->withId($id)->withFilter("dinas")->withType($type); }); Route::get('/kecamatan/{tipeDana?}/tipe/{type?}/id/{id?}', function ($tipeDana = "belanja", $type = "area", $id = null) { return redirect()->route('transparansi')->withDana($tipeDana)->withId($id)->withFilter("kecamatan")->withType($type); }); Route::get('/bumd/{tipeDana?}/tipe/{type?}/id/{id?}', function ($tipeDana = "belanja", $type = "area", $id = null) { return redirect()->route('transparansi')->withDana($tipeDana)->withId($id)->withFilter("bumd")->withType($type); }); Route::get('/other/{tipeDana?}/tipe/{type?}/id/{id?}', function ($tipeDana = "belanja", $type = "area", $id = null) { return redirect()->route('transparansi')->withDana($tipeDana)->withId($id)->withFilter("lain-lain")->withType($type); }); Route::get('/input', ['as' => 'data.input', function () { $lembagas = App\Lembaga::all(); $tags = App\Tag::all(); return view('monitor.new')->withLembagas($lembagas)->withTags($tags); }]); Route::post('/exclude', ['as' => 'exclude', 'uses' => 'DataController@exclude']); Route::post('/input', ['as' => 'data.store', 'uses' => 'AppController@store']);
// Users Route::get('users/edit', 'UsersController@edit'); Route::patch('users/{users}', 'UsersController@update'); // Articles Route::resource('articles', 'ArticlesController'); // Tags Route::get('tags/{tags}', 'TagsController@show'); // Route::get('articles', 'ArticlesController@index'); // Route::get('articles/{id}', 'ArticlesController@show')->where('id', '[0-9]+'); // Route::get('articles/{id}/edit', 'ArticlesController@edit')->where('id', '[0-9]+'); // Route::get('articles/create', 'ArticlesController@create'); // Route::post('articles', 'ArticlesController@store'); // Api v1 Route::group(['prefix' => 'api/v1', 'as' => 'api.v1::'], function () { Route::get('tags', ['as' => 'tags', function () { return App\Tag::all(); }]); Route::post('articles/generate-slug', ['as' => 'articles.generate-slug', 'uses' => 'api\\v1\\ArticlesController@postGenerateSlug']); }); // Dashboard Route::group(['prefix' => 'dashboard', 'as' => 'dashboard::'], function () { Route::get('/', ['as' => 'index', function () { return 'dashboard'; }]); Route::get('/users', function () { return 'all users'; }); }); // Event get('broadcast', function () { event(new App\Events\UserHasRegistered('lol'));
<?php /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define(App\User::class, function (Faker\Generator $faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define(App\Post::class, function (Faker\Generator $faker) { return ['title' => $faker->sentence, 'resume' => $faker->paragraph, 'text' => $faker->realText(2000)]; }); $factory->define(App\Tag::class, function (Faker\Generator $faker) { return ['name' => $faker->word]; }); $factory->define(App\Comment::class, function (Faker\Generator $faker) { $posts = App\Post::all()->lists('id'); return ['post_id' => $faker->randomElement($posts->all()), 'comment' => $faker->paragraph, 'name' => $faker->name, 'email' => $faker->email]; }); $factory->define(App\PostHasTag::class, function (Faker\Generator $faker) { $posts = App\Post::all()->lists('id'); $tags = App\Tag::all()->lists('id'); return ['post_id' => $faker->randomElement($posts->all()), 'tag_id' => $faker->randomElement($tags->all())]; });