0
votes

I would like to update an old table that have a composite primary key. In order to generate a full update query such as:

UPDATE foos SET (...) WHERE pk1 = ? AND pk2 = ?; 

Instead Eloquent tries to do the following which does massive update:

UPDATE foos SET (...) WHERE pk1 = ?; 

I set the primary key attribute to null as seen on Google:

class Foo extends Model {
    protected $guarded = [];
    protected $primaryKey = null;
    public $incrementing = false;
    public $timestamps = false;
}

But I get this error:

Illuminate\Database\QueryException: SQLSTATE[42S22]: 
Column not found: 1054 Unknown column '' in 'where clause' 
(SQL: update `previews` set `preview_id` = 1805 where `` is null) in 
./vendor/illuminate/database/Connection.php on line 664

I suppose this is not a supported feature of Eloquent, so how can I generate my update query?

My update query was:

    $p = Foo
        ::where('pk1', $foo)
        ->where('pk2', $bar)
        ->first();

    $p->update('column', 42);
1
What's your UPDATE query?Jonas Staudenmeir
@JonasStaudenmeir I updated my questionnowox

1 Answers

2
votes

Don't fetch the model, update it directly in the database:

Foo::where('pk1', $foo)
    ->where('pk2', $bar)
    ->update(['column' => 42]);