firstOrNew 如何在数据库中查找记录?

How does firstOrNew looks for records on the database?

我的 table:

已有记录
company_id       first_name      last_name         age    
DCI-201812001      John             Doe            27

我想要的是,如果我插入新记录,我想通过检查 first_namelast_name 和 [=14= 来检查记录是新的还是要更新的].

我在Laravel中使用了firstOrNew方法,然而,当我试图用ff插入一条记录时。值:

first_name = "John"
last_name = "Doe"
age = 29

Laravel的firstOrNew认为和我上面的现有记录一样

这是我的控制器:

截图

  1. 这是我插入与现有记录有些相同的记录时检索到的内容。 first_name = "John"last_name = "Doe"age = 29。它检索了现有记录。当年龄列不同时,为什么它会检索现有记录?为什么它不考虑我过去的年龄?




Please disregard question number 2. I realized that if record is not found, it returns a new instance of the model.

  1. 这是我插入完全不同的记录时检索到的内容。 first_name = "Felicita"last_name = "Ignacio"age = 25。当我在表单上的年龄列上传递值 25 时,为什么年龄列变为空值?

您的 first_namelast_nameage 需要像这样:

[
    'first_name' => ...,
    'last_name' => ...,
    'age' => ...,
]

不是这样的:

['first_name' => ...],
['last_name' => ...],
['age' => ...],

您要发送三个单独的数组作为三个单独的参数。您应该发送一个数组作为 first 参数。

firstOrNew 的第二个参数是在 orNew 部分执行时要设置的附加默认值数组。)