【问题标题】:Eloquent: Create relation if not existEloquent:如果不存在则创建关系
【发布时间】:2020-12-25 08:48:14
【问题描述】:

订阅表:id、client_id、status、valid_until 客户名称:id, name

模型Client.php:

public function subscription { 
  return $this->hasOne(Subscription::class) 
}

我想使用$client->subscription->start() 来:

  1. 如果之前的关系已经存在:status=1, valid_until \now()+addDays(30)
  2. 如果关系不存在,则使用上述数据创建订阅行。

有可能吗? 还是我必须在subscription.php 中构建一个静态函数。例如:Subscription::start($client)

谢谢

【问题讨论】:

    标签: laravel eloquent eloquent-relationship


    【解决方案1】:

    我认为您需要像 https://laravel.com/docs/7.x/eloquent-relationships#default-models 这样的东西来适应您的要求。

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 2022-09-25
      • 2012-02-04
      • 2022-07-20
      • 1970-01-01
      • 2013-05-26
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多