Laravel Eloquent no primary key

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:
IlluminateDatabaseQueryException: 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)

    $p->update('column', 42);


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x