/** * Run the database seeds. * * @return void */ public function run() { $shuffle = function ($tags, $num) { $s = []; shuffle($tags); while ($num >= 0) { $s[] = $tags[$num]; $num--; } return $s; }; $max = $this->tag->count(); $tags = $this->tag->lists('id')->toArray(); // passé d'une collection à un array factory(App\Product::class, 15)->create()->each(function ($product) use($shuffle, $max, $tags) { $product->tags()->attach($shuffle($tags, rand(1, $max - 1))); App\Score::create(['number_command' => rand(1, 100), 'product_id' => $product->id, 'score' => rand(1, 10)]); }); }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', 'PageController@index'); Route::get('statistikk', 'PageController@statistikk'); Route::get('game', 'PageController@footballGame'); Route::get('class', 'LectureController@index'); Route::get('game/scores', function () { return App\Score::all(); }); Route::post('game/scores', function () { App\Score::create(Request::all()); }); Route::get('admin', 'AdminController@index'); Route::post('admin', 'AdminController@login');