我的Schedules表中的“date”字段有一个mutator设置。当我使用它的模型直接从中获取数据时,一切正常。
但是,当我将Schedules表连接到查询中的另一个表时,如何更改“date”字段?
示例:我正在对我的GameRequests表运行查询并将Schedules表连接到它,但是日期字段没有被变异。它仍处于“YYYY-MM-DD”格式。
My Mutator里面的Schedule Model:
/**
* Always format date to Month / Day / Year when retrieving.
*/
public function getDateAttribute($value) {
return date('M d, Y', strtotime($value));
}
以下作品非常完美:
$list = Schedule::where('team_id', $id)->orderBy('date')->get();
return $list->toJson();
以下不是因为我没有调用Schedule Model:
$list = GameRequest::whereIn('team_to', $user_teams)
->join('teams as team_to', 'game_requests.team_to', '=', 'team_to.id')
->join('teams as team_from', 'game_requests.team_from', '=', 'team_from.id')
->join('schedules', 'game_requests.schedule_id', '=', 'schedules.id')
->select(
'game_requests.id as gr_id',
'game_requests.team_to',
'game_requests.team_from',
'team_to.display_name as to_name',
'team_from.display_name as from_name',
'schedules.date'
)
->orderBy('date')
->get();
return $list->toJson();
答案 0 :(得分:0)
您可以将DATE_FORMAT
功能与selectRaw
一起使用,以获得所需格式的日期。
$list = GameRequest::whereIn('team_to', $user_teams)
->join('teams as team_to', 'game_requests.team_to', '=', 'team_to.id')
->join('teams as team_from', 'game_requests.team_from', '=', 'team_from.id')
->join('schedules', 'game_requests.schedule_id', '=', 'schedules.id')
->selectRaw(
'game_requests.id as gr_id',
'game_requests.team_to',
'game_requests.team_from',
'team_to.display_name as to_name',
'team_from.display_name as from_name',
'DATE_FORMAT(schedules.date,'%b %d, %Y') as date'
)
->orderBy('date')
->get();
return $list->toJson();