【问题标题】:How to format a relationship date?如何格式化关系日期?
【发布时间】:2022-01-26 05:28:54
【问题描述】:

我有这个要求

$marche = March::with('attribution')->get()->where('attribution.date_attribution', '2021');

如何格式化“attribution.date_attribution”以仅检索年份?

我尝试使用 Carbon::parse 但出现以下错误:

Could not parse 'attribution.date_attribution': Failed to parse time string (attribution.date_attribution) at position 0 (a): The timezone could not be found in the database

【问题讨论】:

  • attribution.date_attribution 的当前格式是什么?
  • 它已经在归因模型中格式化:protected $dates = ['deleted_at', 'date_attribution'];

标签: laravel eloquent


【解决方案1】:

未经测试,但你可以试试这个:

$marche = March::
  with('attribution')
->whereHas('attribution', function($query){
    $query->whereYear('date_attribution', '2021');
  })
->get();

【讨论】:

  • 效果不错,谢谢
【解决方案2】:

不妨试试这个:

->whereYear('attribution.date_attribution', '2021')

https://laravel.com/docs/8.x/queries#additional-where-clauses

【讨论】:

  • 我收到了这个BadMethodCallException Method Illuminate\Database\Eloquent\Collection::whereYear does not exist.
  • 我认为在 get() 函数之前
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多