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...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写