@alex32, just to clarify, do you want to insert only if a condition is met?
And otherwise skip the insert? On a single SQL statement?
The query builder insert method will always try to convert the query to an insert. And as all drivers have the usual insert coded, where statements are ignored.
As you already know, Eloquent's firstOrCreate will execute two queries (one SELECT and one INSERT if needed), I don't believe you can avoid having two queries in case you want to insert something matching a criterion.
MySQL supports INSERT IGNORE, but that would only apply for unique constraints (including primary keys). If that is your case (avoiding inserting a record with an existing primary key), then you could write the raw SQL statement and use DB::statement() to run it.
If all you want is to avoid writing an if clause, you can use the DB::existsOr() method from the query builder:
DB::table('my_table')
->where('id', 57)
->existsOr(fn () => DB::table('my_table')->insert([/*...*/]));
Which internally will run a select to check existence, and will run the callback only if it doesn't exist.
There is a companion DB::doesntExistOr() method, in case I swapped your insert logic.