示例#1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Factory::create('fr_FR');
     $subjectsCount = Subject::all()->count();
     for ($qcms = 0; $qcms < 100; $qcms++) {
         $qcm = Qcm::create(['user_id' => 1, 'subject_id' => rand(1, $subjectsCount - 1), 'name' => $faker->sentence(6), 'description' => $faker->sentence(40)]);
         for ($questions = 0; $questions < rand(5, 10); $questions++) {
             $question = Question::create(['qcm_id' => $qcm->id, 'question' => $faker->sentence(20)]);
             for ($answers = 0; $answers < rand(2, 4); $answers++) {
                 Answer::create(['question_id' => $question->id, 'answer' => $faker->sentence(10), 'isValid' => $answers == 0]);
             }
         }
     }
 }
 public function delete(Request $request, $id)
 {
     $qcm = Qcm::where('id', $id)->with('user', 'questions')->first();
     if (Auth::id() == $qcm->user->id) {
         $ret = DB::transaction(function () use($qcm) {
             foreach ($qcm->participations as $participation) {
                 $participation->delete();
             }
             foreach ($qcm->questions as $question) {
                 foreach ($question->answers as $answer) {
                     $answer->delete();
                 }
                 $question->delete();
             }
             $qcm->delete();
             return true;
         });
         if ($ret) {
             Session::push('messages', 'success|Le QCM a bien été supprimé');
         } else {
             Session::push('messages', "danger|Le QCM n'a pas été supprimé");
         }
         return redirect(route('qcm::mine'));
     } else {
         Session::push('messages', 'danger|Vous ne pouvez pas supprimer le QCM d\'un autre professeur');
         Auth::logout();
         return redirect(route('index'));
     }
 }