:::

17-3 修改考試的儲存動作

  1. 修改控制器\專案\app\Http\Controllers\TestController.php
    public function store(Request $request)
    {
        $content = collect($request->ans)->toJson();
        $score   = 0;
        foreach ($request->ans as $topic_id => $ans) {
            $topic = Topic::find($topic_id);
            $score += ($topic->ans == $ans) ? 20 : 0;
        }
    
        $class_info = session('pref/language');
        $test       = Test::create([
            'content' => $content,
            'user_id' => $request->user_id,
            'exam_id' => $request->exam_id,
            'score'   => $score,
            'grade'   => substr($class_info, 0, 2),
            'class'   => substr($class_info, 2, 2),
            'num'     => substr($class_info, 4, 2),
        ]);
        return redirect()->route('test.show', $test->id);
    }

     

  2. 修改考試的模型\專案\app\Test.php,主要是修改$fillable新增加入的欄位
    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Test extends Model
    {
        protected $fillable = [
            'content', 'user_id', 'exam_id', 'score','grade','class','num',
        ];
    
        public function exam()
        {
            return $this->belongsTo('App\Exam');
        }
    
        public function user()
        {
            return $this->belongsTo('App\User');
        }
    }
    

     


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D37%26tbdsn%3D1329

計數器

今天: 1516151615161516
昨天: 1416141614161416
總計: 7340163734016373401637340163734016373401637340163