我收到此錯誤「errorstimes in scoresheetController.php 259行: 當我想將以下內容發佈到數據庫中時,Illuminate \ Database \ Eloquent \ Collection類的對象無法轉換爲int」 。請在下面找到代碼。當我嘗試發佈到數據庫時發生錯誤。我試圖計算GPA
<?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();
}
}
如果能夠儘快給予幫助,我會很高興。
控制器過於肥胖,並且這個問題的代碼太多。縮小你的問題。 – Devon
你可以請你發佈更多的錯誤,你會得到。可能你應該已經得到了代碼中的真正問題的行號。 –