如何判断`model`中的数据是否存在数据库中?
在tinker
中做一个测试,id
是20有数据,21
是没有数据的,数据如下:
>>> App\Model\Test\Tname::find(20);
=> App\Model\Test\Tname {#4442
id: 20,
name: "jd",
age: 18,
}
>>> App\Model\Test\Tname::find(21);
=> null
我声明一个不存在的模型model
,只赋值不保存入库;之后通过exists
来进行判断,结果返回的是true
,如下:
>>> $test = new App\Model\Test\Tname;
=> App\Model\Test\Tname {#4447}
>>> $test->id = 21;
=> 21
>>> $test->name = 'test';
=> "test"
>>> $test->age = 19;
=> 19
>>> $test->exists();
=> true
而我预期希望实现的是如果模型中的数据是手动赋值而并不存在数据库中,能够返回false
,请问,我该如何知道model
中的数据的确是存在数据库中的呢?
推荐文章: