firstorcreate 不适用于新条目 laravel
firstorcreate is not working for new entry laravel
我是 laravel 的新手,使用 ardent 创建模型,我的所有模型都低于基础 class。
class MainModel extends Ardent {
//with some common methods
}
现在我有子模型class这样的用户
class User extends MainModel
{
//with user table and other methods related to user
}
我还有 user ->User Repository 的存储库,我正在调用 firstOrcreate 方法来检查(用户输入)和创建用户。
如果用户存在于数据库中,那么上面的方法工作正常,它返回现有的用户对象,但如果用户不存在于数据库中,它不会创建用户,也不会插入条目 table.is 我在这里做错了吗?
你只需要在调用firstOrCreate()
后保存用户:
$user->save();
您可能需要检查用户是否在此之前不存在:
if (!$user->id) $user->save();
我是 laravel 的新手,使用 ardent 创建模型,我的所有模型都低于基础 class。
class MainModel extends Ardent {
//with some common methods
}
现在我有子模型class这样的用户
class User extends MainModel
{
//with user table and other methods related to user
}
我还有 user ->User Repository 的存储库,我正在调用 firstOrcreate 方法来检查(用户输入)和创建用户。 如果用户存在于数据库中,那么上面的方法工作正常,它返回现有的用户对象,但如果用户不存在于数据库中,它不会创建用户,也不会插入条目 table.is 我在这里做错了吗?
你只需要在调用firstOrCreate()
后保存用户:
$user->save();
您可能需要检查用户是否在此之前不存在:
if (!$user->id) $user->save();