#Eloquent executes 2nd query without where statement when doing MyModel::find($id)

1 messages · Page 1 of 1 (latest)

noble flower
#

Hey folks, I must be doing something majorly wrong but it seems the ORM is doing 2 queries with this simple code:

$account = Account::find(2)->firstOrFail();

I debugged this via

DB::connection()->enableQueryLog();
$account = Account::find(2)->firstOrFail();
$queries = DB::getQueryLog();

and $queries contains 2 entries as you can see here

array (
  0 => 
  array (
    'query' => 'select * from `accounts` where `accounts`.`id` = ? limit 1',
    'bindings' => 
    array (
      0 => 2,
    ),
    'time' => 0.11,
  ),
  1 => 
  array (
    'query' => 'select * from `accounts` limit 1',
    'bindings' => 
    array (
    ),
    'time' => 0.09,
  ),
)

No idea where the 2nd query comes from but it causes my query to only return the first item from the table, no matter what ID I pass onto the model.

Here is my model file:

class Account extends Model
{
    use HasFactory;

    protected $table = 'accounts';

    protected $fillable = ['name'];

    public function bankEntries(): HasMany
    {
        return $this->hasMany(BankEntry::class);
    }
}

I just recently updated laravel to v11.7.0 but I doubt its related to that

#

Eloquent executes 2nd query without where statement when doing MyModel::find($id)

undone seal
#

find() already returns the model

noble flower
#

damn, thats it