You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目日期存入MySQL显示为'0000-00-00'问题求助

问题描述

我需要将指定日期范围内的每个单独日期存入数据库,但尽管控制器能正确接收到日期参数,存入MySQL的日期却显示为0000-00-00格式。

控制器代码

public function store(Request $request)
{
    // dd($request->all());
    $validator = Validator::make($request->all(), [
        'title' => 'required|max:191',
        'start_date' => 'required',
    ]);
    
    if ($validator->fails()) {
        if($request->ajax()){ 
            return response()->json(['result'=>'error','message'=>$validator->errors()->all()]);
        }else{
            return redirect('holidays/create')
                        ->withErrors($validator)
                        ->withInput();
        }           
    }
        
  
    $startDate = new Carbon($request->start_date);
    $endDate = new Carbon($request->end_date);
    $all_dates = array();
    while ($startDate->lte($endDate)){
      $all_dates[] = $startDate->toDateString();
    
      $startDate->addDay();
    }
    // dd($all_dates);
    if(isset($request->start_date)){
        foreach($all_dates as $holdate){
            $holiday= new Holiday();
            $holiday->title = $request->input('title');
            $holiday->start_date = $holdate;
            $holiday->end_date = $request->input('end_date');
            $holiday->company_id = company_id();
            // dd($holiday);
            $holiday->save();

        }
    }

  
    
    
    //Set Related Data
    
    if(! $request->ajax()){
       return redirect('holidays')->with('success', _lang('Saved Sucessfully'));
    }else{
       return response()->json(['result'=>'success','action'=>'store','message'=>_lang('Saved Sucessfully'),'data'=>$account]);
    }
}

Blade页面表单代码

{{ csrf_field() }}

<div class="col-md-12">
  <div class="form-group">
    <label class="control-label">{{ _lang('Title').'('._lang('Holiday For').')'}}</label>                      
    <input type="text" class="form-control" name="title" value="{{ old('title') }}" required>
  </div>
</div>

<div class="col-md-12">
  <div class="form-group">
    <label class="control-label">{{ _lang('Start Date') }}</label>                      
    <input type="text" class="form-control datepicker" name="start_date" value="{{ old('start_date') }}" required>
  </div>
</div>

<div class="col-md-12">
  <div class="form-group">
    <label class="control-label">{{ _lang('End Date') }}</label>                         
    <input type="text" class="form-control datepicker" name="end_date" value="{{ old('end_date') }}">
  </div>
</div>
        
<div class="form-group">
  <div class="col-md-12">
    <button type="reset" class="btn btn-danger">{{ _lang('Reset') }}</button>
    <button type="submit" class="btn btn-primary">{{ _lang('Save') }}</button>
  </div>
</div>
</form>
解决方案

1. 检查模型批量赋值权限

确保Holiday模型中配置了$fillable属性,包含需要写入的字段:

class Holiday extends Model
{
    protected $fillable = ['title', 'start_date', 'end_date', 'company_id'];
}

未设置$fillable会导致Laravel阻止字段赋值,最终数据库字段为空,显示0000-00-00。

2. 完善日期验证规则

修改验证逻辑,确保end_date有效且不早于start_date,同时处理空值情况:

$validator = Validator::make($request->all(), [
    'title' => 'required|max:191',
    'start_date' => 'required|date',
    'end_date' => 'nullable|date|after_or_equal:start_date',
]);

// 若end_date为空,默认设为start_date
$endDate = $request->filled('end_date') ? new Carbon($request->end_date) : new Carbon($request->start_date);

3. 确认数据库字段类型

检查MySQL中holidays表的start_date和end_date字段类型为DATE或DATETIME,避免因类型不匹配导致日期被转换为0000-00-00。

4. 优化日期赋值逻辑

直接使用Carbon实例赋值,Laravel会自动转换为数据库兼容格式:

foreach($all_dates as $holdate){
    $holiday = new Holiday();
    $holiday->title = $request->input('title');
    $holiday->start_date = Carbon::parse($holdate);
    $holiday->end_date = $request->filled('end_date') ? Carbon::parse($request->end_date) : null;
    $holiday->company_id = company_id();
    $holiday->save();
}

5. 调试赋值结果

取消注释控制器中的dd($holiday);,查看实例的日期字段是否为正确值,排查赋值阶段的问题。


内容的提问来源于stack exchange,提问作者Mizbah Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:17:00