【问题标题】:firstorcreate is not working for new entry laravelfirstorcreate 不适用于新条目 laravel
【发布时间】:2015-10-28 01:19:19
【问题描述】:

我是 laravel 的新手,使用 ardent 创建模型,我有以下所有模型的基类。

class MainModel extends Ardent {
  //with some common methods
}

现在我有子模型类这样的用户

class User extends MainModel
{
  //with user table and other methods related to user
 }

我还有 user ->User Repository 的存储库,我正在调用 firstOrcreate 方法来检查(用户条目)和创建用户。 如果用户存在于数据库中,则上述方法工作正常,它返回现有的用户对象,但如果用户不存在于数据库中,则不会创建用户,也不会向表中插入条目。我在这里做错了吗?

【问题讨论】:

  • 您的日志文件能否深入了解问题所在?

标签: laravel-4 laravel-5 ardent


【解决方案1】:

调用firstOrCreate()后只需要保存用户即可:

$user->save();

您可能需要检查用户在此之前是否不存在:

if (!$user->id)  $user->save();

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2019-10-30
    • 2021-04-28
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多