Hi @rotaercz ,
It is possible to change the primary key of an Eloquent model overwriting the property defined in the base model.
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'root_id';
On the other hand, it is possible to use firstOrNew method using two arguments as defined @ https://laravel.com/docs/5.5/eloquent#other-creation-methods. You will provide as first argument the values to find and a second argument with the values to set if not found.
$c = Comments::firstOrNew([
'topic' => $topic,
'parent_id' => $parent_id
], [
'root_id' => $root_id,
'username' => $username,
'comment' => $comment
]);
Please note that the firstOrNew do not persist the record in the database as firstOrCreate does.
Hope this helps.
Regards, Adrian