dongyi6183 2017-06-04 20:11
浏览 47
已采纳

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

I am getting this error "ErrorException in scoresheetController.php line 259: Object of class Illuminate\Database\Eloquent\Collection could not be converted to int" when I want to post the following into the database. Kindly find the code below.

 <?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();
        }

    }

I would be glad if assistance can be given as soon as possible.

  • 写回答

2条回答 默认 最新

  • douxiaomang5640 2017-06-04 23:01
    关注

    Since $crdthr is a Collection object, you can print it to see the data format in there. Usually, you can get the data you are looking for as $crdthr[0]-> credit_hrs

    $gpa = $digit * $crdthr[0]->credit_hrs;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面