Advertisement
irobust

Laravel Poll Database

Oct 7th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. #### Migration #####
  2. Schema::create('polls', function (Blueprint $table) {
  3.             $table->increments('id');
  4.             $table->string('title', 200);
  5.             $table->timestamps();
  6.         });
  7.  
  8.         Schema::create('questions', function (Blueprint $table) {
  9.             $table->increments('id');
  10.             $table->string('title', 200);
  11.             $table->mediumText('question');
  12.             $table->unsignedInteger('poll_id');
  13.             $table->timestamps();
  14.         });
  15.  
  16.         Schema::create('answers', function (Blueprint $table) {
  17.             $table->increments('id');
  18.             $table->mediumText('answer');
  19.             $table->unsignedInteger('question_id');
  20.             $table->timestamps();
  21.         });
  22.  
  23.  
  24.  
  25. #### Database Seeder ######
  26.  
  27. factory(App\User::class, 5)->create();
  28. factory(App\Poll::class, 10)->create();
  29. factory(App\Question::class, 50)->create();
  30. factory(App\Answer::class, 500)->create();
  31.  
  32. #### Poll Factory #####
  33. $factory->define(App\Poll::class, function (Faker $faker) {
  34.     return [
  35.         'title' => $faker->realText(50),
  36.     ];
  37. });
  38.  
  39. $factory->define(App\Question::class, function (Faker $faker) {
  40.     $poll_ids = DB::table('polls')->pluck('id')->all();
  41.  
  42.     return [
  43.         'title' => $faker->realText(50),
  44.         'question' => $faker->realText(500),
  45.         'poll_id' => $faker->randomElement($poll_ids),
  46.     ];
  47. });
  48.  
  49. $factory->define(App\Answer::class, function (Faker $faker) {
  50.     $question_ids = DB::table('questions')->pluck('id')->all();
  51.  
  52.     return [
  53.         'answer' => $faker->realText(500),
  54.         'question_id' => $faker->randomElement($question_ids),
  55.     ];
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement