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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大