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

Samer_J's avatar

Parameter randomly being added to SQL query?

I'm trying to run the following query:

CompetitionMatch::where('competition_id', $competition->id)->where('id', $match->id)->update(['posted_at' => Carbon::now()]);

But I get the following error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (SQL: update `competition_matches` set `posted_at` = 2017-09-10 22:02:11 where `competition_id` = App\CompetitionMatch and `id` = 1)

From inspecting the error I can see that 'App\CompetitionMatch' gets added to the list of parameters for some reason:

'update `competition_matches` set `posted_at` = ? where `competition_id` = ? and `id` = ?', array(object(Carbon), 'App\\CompetitionMatch', 1, 14

The first parameter is Carbon::now(). The last two (1, 14) are my competition and match id's. But why is it adding the 'App\CompetitionMatch' as a second parameter? There's literally no reason for this to be happening.

0 likes
8 replies
Snapey's avatar

is $competition initialised correctly?

If id is the primary key then you can just set the timestamp on the model, no need for competition_id at all?

The name of the model and the table implies this is a pivot table so I am puzzled why you have an id column and not a match_id column?

Samer_J's avatar

It's not a pivot table. The model is simply called CompetitionMatch. Otherwise the table name would've been competition_match.

The table's primary key is 'id' with a foreign key 'competition_id'.

You are right about the competition id. No need for it. But even after removing it I'm still getting the exact same issue.

This is my query now:

CompetitionMatch::where('id', $match->id)->update(['posted_at' => Carbon::now()]);
Snapey's avatar

'update competition_matches

looks like a pivot table to me, and likely to Eloquent also

I suppose strictly speaking it would be competitions_matches

So you are saying that not referencing competition_id in the query it still inserts reference to the model?

Samer_J's avatar

This is the table structure: https://i.gyazo.com/7f90592304908b212a84c2656aff1795.png

It's not setup as a pivot table. The model is called CompetitionMatch. By default Eloquent assumes the table name is going to be competition_matches, which is what I've named it as. I didn't have to explicitly define what the table name was.

It references the model type ('App\CompetitionMatch') and I'm trying to figure out why.

Snapey's avatar

What error is it throwing now? I'm confused because it was doing;

wherecompetition_id= App\CompetitionMatch

but you say you have removed that where statement?

Samer_J's avatar

Error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (SQL: update `competition_matches` set `posted_at` = 2017-09-11 02:49:07 where `id` = App\CompetitionMatch)

Query:

at Connection->runQueryCallback('update `competition_matches` set `posted_at` = ? where `id` = ?', array(object(Carbon), 'App\\CompetitionMatch', 14), object(Closure)) in Connection.php line 607

As you can see it still adds 'App\CompetitionMatch' as a parameter in the query and I seriously can't understand why.

Snapey's avatar

I built something in 5.5;

model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CompetitionMatch extends Model
{
    public $timestamps=false;
}

migration

    public function up()
    {
        Schema::create('competition_matches', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('competition_id')->unsigned();
            $table->integer('round_id')->unsigned();
            $table->integer('group_id');
            $table->timestamp('posted_at');
        });
    }

(I didn't bother with all the columns)

Migrated, then in Tinker;

>>> App\CompetitionMatch::where('id', 1)->update(['posted_at' => Carbon\Carbon::now()]);

And this sets the posted date correctly.

Can you see anything different here to what you are doing?

Samer_J's avatar

I don't see any differences. This is so strange. :/

Please or to participate in this conversation.