dqo88037 2015-01-14 01:12
浏览 39
已采纳

Laravel 4.2 - 使用:: firstOrCreate并确定实际发生了什么?

When using Model::firstOrCreate in Laravel 4.2, is there a way of knowing which happened,, first or create?

Let me explain,,, My understanding is that with ::firstOrCreate a record is created IF it doesn't exist OR values of records containing argument array data are returned.

So if I have something like this:

// Auth::user()->id; returns 7

$person = Persons::firstOrCreate(array(
    'approver' => 123,
    'receiver' => Auth::user()->id,
    'plan' => 'gold',
));

And I have these records

  approver | receiver  |  plan
____________________________________
    123    |     7     |  silver
____________________________________
    128    |     7     |  gold
____________________________________
    123    |     7     |  platinum
____________________________________

Then a new record will be created

  approver | receiver  |  plan
____________________________________
    123    |     7     |  silver
____________________________________
    128    |     7     |  gold
____________________________________
    123    |     7     |  platinum
____________________________________
    123    |     7     |  gold
____________________________________

And if I run the above code again then the new value will not be created and this record will be returned

  approver | receiver  |  plan
____________________________________

    123    |     7     |  gold
____________________________________

But if my code looks like this

$person = Persons::firstOrCreate(array(
    'approver' => 123,
    'receiver' => Auth::user()->id
));

These records will be returned

  approver | receiver  |  plan
____________________________________
    123    |     7     |  silver
____________________________________
    123    |     7     |  platinum
____________________________________
    123    |     7     |  gold
____________________________________

If my understanding of ::firstOrCreate is not correct please help me understand it better.

And as I mentioned,, I am interested to know if there is a way to determine if CREATE of new record happened or not?

  • 写回答

1条回答 默认 最新

  • drxd54816 2015-01-14 01:58
    关注

    The model has a wasRecentlyCreated property:

    $person = Persons::firstOrCreate([
        'approver' => 123,
        'receiver' => auth()->id(),
        'plan' => 'gold',
    ]);
    
    if ($person->wasRecentlyCreated) {
        // $person was created
    } else {
        // $person was fetched
    }
    

    For older versions of Laravel, you can achieve what you want with firstOrNew:

    $person = Persons::firstOrNew([
        'approver' => 123,
        'receiver' => Auth::user()->id,
        'plan' => 'gold',
    ]);
    
    if (! $person->id) {
        $person->save();
    
        // $person was created...
    } else {
        // $person was fetched...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴