如何判断`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中的数据的确是存在数据库中的呢?


关于 LearnKu
推荐文章: