R
R
Roman2019-02-23 00:08:22
Laravel
Roman, 2019-02-23 00:08:22

What is the practical meaning of the firstOrCreate() function (it's better to do findOrCreate())?

Hello.
Can you give a real example when the firstOrCreate() function is needed?
It's just that I'm bewildered: it seems to me it would be more logical to do findOrCreate () (which, as I understand it, does not exist at all).
Why such an emphasis on first?
That is, you usually make a request and if you don’t find it, then you create it, right?
find Or Create :)
And why first instead of find?
I don't get the logic.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-02-23
@procode

Because there is already a method find()that searches STRICTLY by id and returns one model, while firstOrCreate()searching by ..? correctly, by a set of attributes, and as a result, there may be several models. Therefore, such an emphasis. It is enough to open the code of the method and think a little.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question