如何格式化关系日期?

How to format a relationship date?

我有这个请求

$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

也许试试这个:

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

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

未经测试,但你能试试这个吗:

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