Laravel日期列为何额外显示时间?
Laravel 9日期列显示时间后缀问题
我正在用Laravel 9开发原型,现有7个用户表,多个表包含日期列——这些列都是Laravel日期类型,数据库里是MySQL的date类型。但其中一个日期列在Blade模板里显示时,会在正常日期后附加空格和00:00:00,其他所有日期列都只显示YYYY-MM-DD格式,没有时间部分。
我不想让这个列显示始终为00:00:00的时间。Laravel为什么会出现这种情况?怎么解决?
我知道可以用->format('Y-m-d')来隐藏时间,但这不应该是必须的——既然我存的是日期而非DateTime或Timestamp,这个列应该只包含日期,不需要手动处理时间。
更新内容
迁移文件
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('non_driving_weekdays', function (Blueprint $table) { $table->date('non_driving_weekday_date'); $table->string('reason', 50); $table->timestamps(); $table->primary('non_driving_weekday_date', 'non_driving_weekday_date_PK'); $table->foreign('reason', 'reason_FK')->references('reason_for_not_driving')->on('non_driving_reasons')->onUpdate('cascade')->onDelete('restrict'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('non_driving_weekdays'); } };
列表页Blade文件
<x-layout> <div class="bg-gradient-to-b from-red-500 to-purple-100"> <h1 class="text-3xl text-center text-white font-bold bg-indigo-700">Non-driving Weekdays - Index</h1> <h1 class="pb-5"></h1><!-- spacer beneath title --> @if (session()->has('message')) <div id="message" x-data="{show: true}" x-init="setTimeout(() => show = false, 5000)" x-show="show" class="w-1/3 mx-auto text-center text-black bg-yellow-300 rounded-md"> <p class="text-xl py-1">{{session('message')}}</p> </div> @endif <div class="flex justify-center h-screen" style="display: grid; grid-template-rows: auto auto auto auto; row-gap: 10px; padding: 10px"> <table><tr><td><a href="/nonDrivingWeekdays/create"><img src="{{asset('icons/green-plus-circle-outline.svg')}}" width="50" alt="add icon"></a></td><td class="font-bold"><a href="/nonDrivingReasons/create">Add a new date where the drivers didn't drive</a></td></tr></table> {{-- If there are no non-driving weekdays, indicate that to the user. --}} @if (count($nonDrivingWeekdays) == 0) <p>No non-driving weekdays found.</p> @else {{-- If there are non-driving weekdays, display them. --}} <div> <table class="border-black border-2 bg-white"> <thead class="border-black border-2 text-white bg-gray-800"> <tr class="content-center"> <th class="border-black border-2 p-2 text-left">Date</th> <th class="border-black border-2 p-2 text-left">Reason</th> <th class="border-black border-2 p-2">View</th> <th class="border-black border-2 p-2">Edit</th> <th class="border-black border-2 p-2">Delete</th></tr> </thead> <tbody class="border-black border-2"> @foreach($nonDrivingWeekdays as $nonDrivingWeekday) <tr class="border-black border-2"> {{-- ->format('Y-m-d') --}} <td class="border-black border-2 p-2">{{$nonDrivingWeekday->non_driving_weekday_date}}</td> <td class="border-black border-2 p-2">{{$nonDrivingWeekday->reason}}</td> <td class="border-black border-2 p-2 text-center"><a href="/nonDrivingWeekdays/show/{{$nonDrivingWeekday->non_driving_weekday_date}}"><img src="{{asset('icons/magenta-details.svg')}}" width="25" alt="view icon"></a></td> <td class="border-black border-2 p-2 text-center"><a href="/nonDrivingWeekdays/edit/{{$nonDrivingWeekday->non_driving_weekday_date}}"><img src="{{asset('icons/yellow-lead-pencil.svg')}}" width="25" alt="update icon"></a></td> <td class="border-black border-2 p-2 text-center"> <form method="POST" action="/nonDrivingWeekdays/destroy/{{$nonDrivingWeekday->non_driving_weekday_date}}"> @csrf @method('DELETE') <button><img src="{{asset('icons/red-delete.svg')}}" width="25" alt="delete icon"></button> </form></td> </td> </tr> @endforeach </tbody> </table> </div> @endif <div> <p class="mt-6 p-4">{{$nonDrivingWeekdays->links()}}</p> </div> </div> </div> </x-layout>
详情页Blade文件
<x-layout> {{-- <x-card class="p-10"> --}} <div class="bg-gradient-to-b from-purple-500 to-purple-100 h-screen"> <h1 class="text-3xl text-center text-white font-bold bg-indigo-700">Non-driving Weekdays - Show One Weekday</h1> <h1 class="pb-5"></h1><!-- spacer beneath title --> <form method="GET" enctype="multipart/form-data" class="w-4/5 mx-auto"> <div class="mb-6"> <label for="non_driving_weekday_date" class="inline-block text-lg mb-2">Reason</label> <input type="text" disabled class="border border-gray-200 rounded p-2 w-full" name="non_driving_weekday_date" value="{{$nonDrivingWeekday->non_driving_weekday_date}}"/> </div> <div class="mb-6"> <label for="reason" class="inline-block text-lg mb-2">Reason</label> <input type="text" disabled class="border border-gray-200 rounded p-2 w-full" name="reason" value="{{$nonDrivingWeekday->reason}}"/> </div> <div class="mb-6"> {{-- Cancel submission of the form --}} <a href="/nonDrivingWeekdays/index"><button type="button" class="text-xl text-white bg-black rounded-md py-2 px-4 hover:bg-white hover:text-black hover:outline hover:outline-2 hover:outline-black">Cancel</button></a> </div> </form> {{-- </x-card> --}} </div> </x-layout>
编辑页Blade文件
<x-layout> {{-- <x-card xclass="p-10 max-w-lg mx-auto mt-24"> --}} <div class="bg-gradient-to-b from-yellow-500 to-yellow-100 h-screen"> <h1 class="text-3xl text-center text-white font-bold bg-indigo-700">Non-driving Weekdays - Edit One Weekday</h1> <h1 class="pb-5"></h1><!-- spacer beneath title --> <form method="POST" action="/nonDrivingWeekdays/update/{{$nonDrivingWeekday->non_driving_weekday_date}}" enctype="multipart/form-data" class="w-4/5 mx-auto"> @csrf @method('PUT') <div class="mb-6"> <label for="non_driving_weekday_date" class="inline-block text-lg mb-2">Non-driving weekday date</label> <input type="text" class="border border-gray-200 rounded p-2 w-full" name="non_driving_weekday_date" value="{{$nonDrivingWeekday->non_driving_weekday_date}}"/> @error('non_driving_weekday_date') <p class="text-red-500 font-bold text-m mt-1">{{$message}}</p> @enderror </div> <div class="mb-6"> <label for="reason" class="inline-block text-lg mb-2">Reason</label> <select id="reason" name="reason" class="h-10"> @foreach($reasons as $reason) @if($reason->reason_for_not_driving === $nonDrivingWeekday->reason) <option selected class="mx-10" value="{{$reason->reason_for_not_driving}}">{{$reason->reason_for_not_driving}}</option> @else <option class="mx-10" value="{{$reason->reason_for_not_driving}}">{{$reason->reason_for_not_driving}}</option> @endif @endforeach </select> </div> <div class="mb-6"> {{-- Submit the completed form --}} <button class="text-xl text-white bg-orange-500 rounded-md py-2 px-4 hover:bg-white hover:text-orange-500 hover:outline hover:outline-2 hover:outline-orange-500">Submit</button> {{-- Cancel submission of the form --}} <a href="/nonDrivingWeekdays/index"><button type="button" class="text-xl text-white bg-black rounded-md py-2 px-4 hover:bg-white hover:text-black hover:outline hover:outline-2 hover:outline-black">Cancel</button></a> </div> </form> {{-- </x-card> --}} </div> </x-layout>
模型文件
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class NonDrivingWeekdays extends Model { use HasFactory; protected $fillable = ['non_driving_weekday_date', 'reason']; protected $primaryKey = 'non_driving_weekday_date'; protected $keyType = 'date'; public $incrementing = 'false'; }
问题原因及解决方法
原因分析
问题出在模型的两个配置细节上:
$incrementing属性被错误设置为字符串'false',这个属性应该是布尔值,错误的类型会导致Laravel对主键的处理逻辑异常。- 该日期列同时是主键,Laravel默认处理主键字符串化时,会调用Carbon的完整格式(包含时间),而非仅日期格式,即使数据库里是
date类型也会触发这个问题。
解决步骤
1. 修正$incrementing属性
将模型中的:
public $incrementing = 'false';
修改为布尔值:
public $incrementing = false;
2. 显式指定日期列格式
在模型中添加$casts属性,明确告诉Laravel该列的日期格式:
protected $casts = [ 'non_driving_weekday_date' => 'date:Y-m-d', ];
或者使用日期访问器做更灵活的控制:
protected $dates = ['non_driving_weekday_date']; public function getNonDrivingWeekdayDateAttribute($value) { return $value->format('Y-m-d'); }
3. 验证效果
修改后重新加载页面,non_driving_weekday_date列将只显示YYYY-MM-DD格式,不会再附加00:00:00的时间后缀,无需在Blade模板中手动调用format()方法。
内容的提问来源于stack exchange,提问作者Henry
相关产品推荐
相关产品推荐

