Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aarontharker's avatar

Eloquent is giving me an 'Invalid parameter number' error

This query $sitting = Sitting::where('course_id', $course)->where('year', $row['year'])->where('semester', $row['semester'])->first(); appears to be the line causing the error but I can't see why. The variables are as follows.

$course = 1;
$row['semester'] = 2;
$row['year'] = 2023;
0 likes
4 replies
LaryAI's avatar
Level 58

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:

  1. Ensure that the Sitting model is using the correct database table and that the table has the course_id, year, and semester columns.

  2. Check if there are any global scopes or local scopes applied to the Sitting model that might be interfering with the query.

  3. 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);
  1. 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.

  2. 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.

  3. Make sure that the variables $course, $row['year'], and $row['semester'] are of the correct data type expected by the database. For example, if course_id is an integer in the database, $course should 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.

mike_isp's avatar

Try appending ->toSql() to see if the query looks right. When I do this (I created a Sitting model without a table to test) it outputs select * from sittingswherecourse_id= 1 andyear= 2023 andsemester = 2 limit 1 which looks correct to me.

There may be other code modifying your variables or query. Try isolating this code, maybe to its own method to help debug. If it works in isolation then some other code is the problem.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

you absolutely sure that $course is a plain integer and not an actual course object?

1 like
aarontharker's avatar

@snapey yeah I just found that the $course is actually not returning a plain integer but an array.

Please or to participate in this conversation.