我尝试发布到数据库时出错。我正在尝试计算GPA

时间:2017-06-04 20:11:42

标签: php database laravel

我收到此错误“ScoreheetController.php第259行中的ErrorException: 当我想将以下内容发布到数据库中时,类Illuminate \ Database \ Eloquent \ Collection的对象无法转换为int。请找到下面的代码。

 <?php

    namespace App\Http\Controllers;


    use App\Http\Requests\scoresheetRequest;
    use App\Scoresheet;
    use App\StudentCourse;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Input;
    use DB;
    use Excel;
    use Illuminate\Database\Eloquent\Collection;

    class scoresheetController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
        }

        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $scoresheet = Scoresheet::select('id', 'code', 'stud_id', 'term', 'year', 'CA_Score', 'exam_score', 'total', 'grade')->get();


            return view('admin.results')->with('scoresheet', $scoresheet);
        }

        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function create()
        {
            //
        }

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(scoresheetRequest $request)
        {
            //dd($request ->all());

            $scoresheet = new Scoresheet();

            $crdthr = StudentCourse::select('student_courses')
                                    ->join('scoresheets', 'student_courses.stud_id', '=', 'scoresheets.stud_id')
                                    ->select('student_courses.credit_hrs')
                                    ->get();

            //Calculate total
            $total  = $request['CA_Score'] + $request['exam_score'];

            //Calculate the grade
            switch ($grade = $total) {
                case $total >=93 && $total==100:
                   $grade =  "A+";
                    break;
                case $total>=85:
                    $grade = "A";
                    break;
                case $total >=77:
                    $grade = "B+";
                    break;
                case $total >=70:
                    $grade = "B";
                    break;
                case $total >=60:
                    $grade = "C";
                    break;
                case $total >=55:
                    $grade = "D+";
                    break;
                case $total >=50:
                    $grade = "D";
                    break;
                case $total <50:
                    $grade = "F";
                    break;
            }

            //Check for grade equivalent
            switch ($digit = $grade) {
                case $grade == "A+":
                    $digit =  "4.00";
                    break;
                case $grade == "A":
                    $digit =  "3.75";
                    break;
                case $grade == "B+":
                    $digit =  "3.50";
                    break;
                case $grade == "B":
                    $digit =  "3.00";
                    break;
                case $grade == "C+":
                    $digit =  "2.50";
                    break;
                case $grade == "C":
                    $digit =  "2.00";
                    break;
                case $grade == "D+":
                    $digit =  "1.50";
                    break;
                case $grade == "D":
                    $digit =  "1.00";
                    break;
                case $grade == "F":
                    $digit =  "0.00";
                    break;
            }

            //Calculate mini wgt
            $gpa = $digit * $crdthr;

            $scoresheet->code = $request['code'];
            $scoresheet->stud_id = $request['stud_id'];
            $scoresheet->term = $request['term'];
            $scoresheet->year = $request['year'];
            $scoresheet->CA_Score = $request['CA_Score'];
            $scoresheet->exam_score = $request['exam_score'];
            $scoresheet->total = $total;
            $scoresheet->grade = $grade;
            $scoresheet->grade_digit = $digit;
            $scoresheet->mini_wgt = $gpa;

            //dd($gpa);
            if ($scoresheet->save()){
                flash($request['name'].' successfully saved.')->success();
                /*echo 'saved';*/
            }else{
                flash($request['name'].' not saved.')->error();
                /*echo 'Not saved';*/
            }

            return redirect()->back();
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            //
        }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function edit($id)
        {
            $scoresheet =Scoresheet::findOrFail($id);

            return view('admin.result_edit') ->with('scoresheet', $scoresheet);
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            $scoresheet = Scoresheet::findOrFail($id);


            $crdthr = StudentCourse::select('student_courses')
                ->join('scoresheets', 'student_courses.stud_id', '=', 'scoresheets.stud_id')
                ->select('student_courses.credit_hrs')
                ->get();

            $total = $request['CA_Score'] + $request['exam_score'];

            //Calculate the grade
            switch ($grade = $total) {
                case $total >=93 && $total==100:
                    $grade =  "A+";
                    break;
                case $total>=85:
                    $grade = "A";
                    break;
                case $total >=77:
                    $grade = "B+";
                    break;
                case $total >=70:
                    $grade = "B";
                    break;
                case $total >=60:
                    $grade = "C";
                    break;
                case $total >=55:
                    $grade = "D+";
                    break;
                case $total >=50:
                    $grade = "D";
                    break;
                case $total <50:
                    $grade = "F";
                    break;
            }

            //Check for grade equivalent
            switch ($digit = $grade) {
                case $grade == "A+":
                    $digit =  "4.00";
                    break;
                case $grade == "A":
                    $digit =  "3.75";
                    break;
                case $grade == "B+":
                    $digit =  "3.50";
                    break;
                case $grade == "B":
                    $digit =  "3.00";
                    break;
                case $grade == "C+":
                    $digit =  "2.50";
                    break;
                case $grade == "C":
                    $digit =  "2.00";
                    break;
                case $grade == "D+":
                    $digit =  "1.50";
                    break;
                case $grade == "D":
                    $digit =  "1.00";
                    break;
                case $grade == "F":
                    $digit =  "0.00";
                    break;
            }


            //Calculate mini wgt
            $gpa = $digit * $crdthr;

            $scoresheet->code = $request['code'];
            $scoresheet->stud_id = $request['stud_id'];
            $scoresheet->term = $request['term'];
            $scoresheet->year = $request['year'];
            $scoresheet->CA_Score = $request['CA_Score'];
            $scoresheet->exam_score = $request['exam_score'];
            $scoresheet->total = $total;
            $scoresheet->grade = $grade;
            $scoresheet->grade_digit = $digit;
            $scoresheet->mini_wgt = $gpa;

            if ($scoresheet->save()){
                flash($request['name'].' successfully saved.')->success();
                /*echo 'saved';*/
            }else{
                flash($request['name'].' not saved.')->error();
                /*echo 'Not saved';*/
            }

            return redirect() ->to('/scoresheet');
        }

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
            $scoresheet = Scoresheet::findOrFail($id);

            if (Scoresheet::destroy($id)){
                flash ('deleted successfully')->success();
            }else{
                flash ('failed to delete')->warning();
            }

            return redirect()->back();
        }

    }

如果能尽快给予帮助,我会很高兴。

2 个答案:

答案 0 :(得分:1)

由于$crdthr是一个Collection对象,您可以打印它以查看其中的数据格式。通常,您可以将您要查找的数据设为$crdthr[0]-> credit_hrs

$gpa = $digit * $crdthr[0]->credit_hrs;

答案 1 :(得分:0)

快速查看代码,我可以说你的问题出现在这段代码中:

$crdthr = StudentCourse::select('student_courses')
   ->join('scoresheets', 'student_courses.stud_id', '=', 'scoresheets.stud_id')
   ->select('student_courses.credit_hrs')
   ->get();

由于您将在下面的代码中返回一个Eloquent Collection,您正试图对此Collection进行数学运算:

   //Calculate mini wgt
   $gpa = $digit * $crdthr;