It seems like your query should work as it is, assuming that Sitting is a valid Eloquent model and the course_id, year, and semester columns exist in the database table associated with the Sitting model. The error message "Invalid parameter number" typically indicates that the number of placeholders in the query does not match the number of supplied parameters.
However, the code you've provided looks correct in terms of parameter binding. To troubleshoot this issue, you can try a few things:
-
Ensure that the
Sittingmodel is using the correct database table and that the table has thecourse_id,year, andsemestercolumns. -
Check if there are any global scopes or local scopes applied to the
Sittingmodel that might be interfering with the query. -
Use the
toSql()method to output the raw SQL query that Eloquent is trying to execute. This can help you understand if the query is being constructed as expected.
Here's how you can output the raw SQL query:
$query = Sitting::where('course_id', $course)
->where('year', $row['year'])
->where('semester', $row['semester'])
->toSql();
dd($query);
-
If you're using prepared statements somewhere else in your code, ensure that they are being handled correctly. A mismatch in prepared statement bindings can cause this error.
-
Check your database logs to see the actual query being executed and the error message. This might give you more insight into what's going wrong.
-
Make sure that the variables
$course,$row['year'], and$row['semester']are of the correct data type expected by the database. For example, ifcourse_idis an integer in the database,$courseshould also be an integer.
If none of these steps help, you might want to provide more context or the exact error message you're receiving, as that could help in diagnosing the issue further.