:::

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%3Ftbdsn%3D1329%26tbsn%3D37

計數器

今天: 2446244624462446
昨天: 4159415941594159
總計: 8069964806996480699648069964806996480699648069964