Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Toughoj's avatar

Disable touches on firstOrCreate (laravel 5.5)

I've got two related models :

1-Answer (consists of 'answer_id' and 'text' attributes in addition to 'created_at' and 'updated_at')

2-AnswerBody

AnswerBody model has:

protected $touches = ['Answer'];

I intend to create an AnswerBody without touching the related Answer in some cases. how do I disable touches on create() or firstOrCreate() on AnswerBody ?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

To save a model without touching pass false to save method:

$model = new Model;

$model->save(['touch' => false]);

Of course setTouchedRelations will work as well:

$model = new Model;
... // do what you need

$model->setTouchedRelations([]);
$model->save();

However you seem to want to use the current available values in the database, you could do something like this

$model->firstOrCreate(
    [
        'id' => 1
    ],
    [
        'updated' => DB::raw('updated_at'),
    ]
);

This basically selects the same previous value and inserts that value again.

I would personally go for the first option which is more readable and understandable ;)

1 like
Toughoj's avatar

@bobbybouwmann , I ended up using your second suggestion :

$model = new Model;
... // do what you need

$model->setTouchedRelations([]);
$model->save(); 

Thank you very much :D

Please or to participate in this conversation.