【问题标题】:How to get the YEAR of the created_at column inside the table using LARAVEL如何使用 LARAVEL 获取表中 created_at 列的年份
【发布时间】:2022-01-03 20:26:45
【问题描述】:

我正在尝试创建一个显示所有驾驶员信息的查询,并且仅在 created_at 列中获取“年份”。谁能帮我?提前致谢

这是响应查询

        "user_id": 45,
        "driver_fname": "Sample",
        "driver_mname": "Sample",
        "driver_lname": "Sample",
        "driver_birthday": "2000-07-28",
        "years_driving_experience": null,
        "years_driving_experience_quicktrans": null,
        "start_year_of_exp_in_driving": null,
        "start_year_of_exp_in_driving_in_quicktrans": null,
        "educational_attainment": null,
        "health_status": null,
        "drug_test": null,
        "marital_status": null,
        "Salary_range": null,
        "license_registration": null,
        "license_expiration": null,
        "license_condition": null,
        "medication_maintenance": null,
        "training": null,
        "driver_height": null,
        "driver_weight": null,
        "weight_status": null,
        "emergency_contact_number": null,
        "home_address_street_number": null,
        "home_address_street": null,
        "home_address_brgy": null,
        "home_address_city": null,
        "work_hours_range": null,
        "work_shift": null,
        "sleep_hours_range": null,
        "smoker": null,
        "created_at": "2021-11-23 03:50:33"

created_at 列应为年份,例如 created_at:"2021"

这是我的查询代码

 $driversInformation = DriverInformation::all(['user_id','driver_fname','driver_mname','driver_lname',
        'driver_birthday','years_driving_experience','years_driving_experience_quicktrans','start_year_of_exp_in_driving',
        'start_year_of_exp_in_driving_in_quicktrans','educational_attainment','health_status','drug_test','marital_status',
        'salary_level As Salary_range','license_registration','license_expiration','license_condition','medication_maintenance',
        'training','driver_height','driver_weight','weight_status','emergency_contact_number','home_address_street_number',
        'home_address_street','home_address_brgy','home_address_city','work_hours_range','work_shift','sleep_hours_range','smoker',
        'created_at As year']);

【问题讨论】:

标签: mysql laravel api


【解决方案1】:

最简单的方法是更改​​ DriverInformation 模型中的日期

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'created_at' => 'datetime:Y',
];

您可以在此处https://laravel.com/docs/8.x/eloquent-mutators#date-casting

阅读有关 mutators 的更多信息

【讨论】:

    【解决方案2】:

    SQL 查询应该是

    select Year(createdAt) as Year from Persons;
    
    

    你可以在这里查看示例https://www.db-fiddle.com/f/d5j8icfiw8nvwZdKqhtr4h/0

    【讨论】:

      【解决方案3】:

      你可以使用 Carbon 类 -> \Carbon\Carbon::parse($item->created_at)->format('Y')

      【讨论】:

      • 我应该把这段代码放在哪里?
      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2017-01-15
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      相关资源
      最近更新 更多