SQLSTATE [22007]:无效的日期时间格式:1292错误的日期时间值:第1行“ {pub44_date}”列的“ 08/22/2018”

时间:2018-08-28 06:12:35

标签: datetime datetimepicker

我正在为管理员创建新闻门户。我想创建一个新闻管理器部分,我已经创建了新闻管理器,但是在我提交表单时,我得到了如下错误: 错误屏幕截图:enter image description here

  

Illuminate \ Database \ QueryException与消息一起抛出   “ SQLSTATE [22007]:无效的日期时间格式:1292错误的日期时间   值:第1行“ publish_date”列的“ 08/22/2018”(SQL:插入   变成newstitlewritershort_desdetail_des,   publish_datestatusslugimageupdated_atcreated_at)   值(尼泊尔·哈巴尔,czczxc,zczzxzxczd,zczcdscsdcssd,2018年8月22日,   1,尼泊尔-卡巴尔,1857_2.jpg,2018-08-28 06:04:52,2018-08-28   06:04:52))“

意见: add.blade.php

@extends('admin.layout.master')

@section('css')
    <link rel="stylesheet" href="{{ asset('admin_panel/assets/css/bootstrap-datepicker3.min.css') }}" />

@endsection

@section('content')

    <div class="main-content">
        <div class="main-content-inner">
            <div class="breadcrumbs ace-save-state" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li>
                        <i class="ace-icon fa fa-home home-icon"></i>
                        <a href="{{ route('admin.dashboard') }}">Home</a>
                    </li>

                    <li>
                        <a href="{{ route($base_route) }}">{{$panel}}</a>
                    </li>
                    <li class="active">Add</li>
                </ul><!-- /.breadcrumb -->

                <div class="nav-search" id="nav-search">
                    <form class="form-search">
                                <span class="input-icon">
                                    <input type="text" placeholder="Search ..." class="nav-search-input" id="nav-search-input" autocomplete="off" />
                                    <i class="ace-icon fa fa-search nav-search-icon"></i>
                                </span>
                    </form>
                </div><!-- /.nav-search -->
            </div>

            <div class="page-content">
                <div class="ace-settings-container" id="ace-settings-container">
                    <div class="btn btn-app btn-xs btn-warning ace-settings-btn" id="ace-settings-btn">
                        <i class="ace-icon fa fa-cog bigger-130"></i>
                    </div>

                    <div class="ace-settings-box clearfix" id="ace-settings-box">
                        <div class="pull-left width-50">
                            <div class="ace-settings-item">
                                <div class="pull-left">
                                    <select id="skin-colorpicker" class="hide">
                                        <option data-skin="no-skin" value="#438EB9">#438EB9</option>
                                        <option data-skin="skin-1" value="#222A2D">#222A2D</option>
                                        <option data-skin="skin-2" value="#C6487E">#C6487E</option>
                                        <option data-skin="skin-3" value="#D0D0D0">#D0D0D0</option>
                                    </select>
                                </div>
                                <span>&nbsp; Choose Skin</span>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-navbar" autocomplete="off" />
                                <label class="lbl" for="ace-settings-navbar"> Fixed Navbar</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-sidebar" autocomplete="off" />
                                <label class="lbl" for="ace-settings-sidebar"> Fixed Sidebar</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-breadcrumbs" autocomplete="off" />
                                <label class="lbl" for="ace-settings-breadcrumbs"> Fixed Breadcrumbs</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-rtl" autocomplete="off" />
                                <label class="lbl" for="ace-settings-rtl"> Right To Left (rtl)</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-add-container" autocomplete="off" />
                                <label class="lbl" for="ace-settings-add-container">
                                    Inside
                                    <b>.container</b>
                                </label>
                            </div>
                        </div><!-- /.pull-left -->

                        <div class="pull-left width-50">
                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-hover" autocomplete="off" />
                                <label class="lbl" for="ace-settings-hover"> Submenu on Hover</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-compact" autocomplete="off" />
                                <label class="lbl" for="ace-settings-compact"> Compact Sidebar</label>
                            </div>

                            <div class="ace-settings-item">
                                <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-highlight" autocomplete="off" />
                                <label class="lbl" for="ace-settings-highlight"> Alt. Active Item</label>
                            </div>
                        </div><!-- /.pull-left -->
                    </div><!-- /.ace-settings-box -->
                </div><!-- /.ace-settings-container -->

                <div class="page-header">
                    <h1>
                        {{ $panel }} Manager
                        <small>
                            <i class="ace-icon fa fa-angle-double-right"></i>
                            Add user form
                        </small>
                    </h1>
                </div><!-- /.page-header -->

                <div class="row">
                    <div class="col-xs-12">

                        <!-- PAGE CONTENT BEGINS -->
                        <div class="page-content">
                            <div class="ace-settings-container" id="ace-settings-container">
                                <div class="btn btn-app btn-xs btn-warning ace-settings-btn" id="ace-settings-btn">
                                    <i class="ace-icon fa fa-cog bigger-130"></i>
                                </div>

                                <div class="ace-settings-box clearfix" id="ace-settings-box">
                                    <div class="pull-left width-50">
                                        <div class="ace-settings-item">
                                            <div class="pull-left">
                                                <select id="skin-colorpicker" class="hide">
                                                    <option data-skin="no-skin" value="#438EB9">#438EB9</option>
                                                    <option data-skin="skin-1" value="#222A2D">#222A2D</option>
                                                    <option data-skin="skin-2" value="#C6487E">#C6487E</option>
                                                    <option data-skin="skin-3" value="#D0D0D0">#D0D0D0</option>
                                                </select><div class="dropdown dropdown-colorpicker">        <a data-toggle="dropdown" class="dropdown-toggle"><span class="btn-colorpicker" style="background-color:#438EB9"></span></a><ul class="dropdown-menu dropdown-caret"><li><a class="colorpick-btn selected" style="background-color:#438EB9;" data-color="#438EB9"></a></li><li><a class="colorpick-btn" style="background-color:#222A2D;" data-color="#222A2D"></a></li><li><a class="colorpick-btn" style="background-color:#C6487E;" data-color="#C6487E"></a></li><li><a class="colorpick-btn" style="background-color:#D0D0D0;" data-color="#D0D0D0"></a></li></ul></div>
                                            </div>
                                            <span>&nbsp; Choose Skin</span>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-navbar" autocomplete="off">
                                            <label class="lbl" for="ace-settings-navbar"> Fixed Navbar</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-sidebar" autocomplete="off">
                                            <label class="lbl" for="ace-settings-sidebar"> Fixed Sidebar</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-breadcrumbs" autocomplete="off">
                                            <label class="lbl" for="ace-settings-breadcrumbs"> Fixed Breadcrumbs</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-rtl" autocomplete="off">
                                            <label class="lbl" for="ace-settings-rtl"> Right To Left (rtl)</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2 ace-save-state" id="ace-settings-add-container" autocomplete="off">
                                            <label class="lbl" for="ace-settings-add-container">
                                                Inside
                                                <b>.container</b>
                                            </label>
                                        </div>
                                    </div><!-- /.pull-left -->

                                    <div class="pull-left width-50">
                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-hover" autocomplete="off">
                                            <label class="lbl" for="ace-settings-hover"> Submenu on Hover</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-compact" autocomplete="off">
                                            <label class="lbl" for="ace-settings-compact"> Compact Sidebar</label>
                                        </div>

                                        <div class="ace-settings-item">
                                            <input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-highlight" autocomplete="off">
                                            <label class="lbl" for="ace-settings-highlight"> Alt. Active Item</label>
                                        </div>
                                    </div><!-- /.pull-left -->
                                </div><!-- /.ace-settings-box -->
                            </div><!-- /.ace-settings-container -->



                            <div class="row">
                                <div class="col-xs-12">
                                    <!-- PAGE CONTENT BEGINS -->
                                    {!! Form::open(['route' => $base_route.'.store', 'method' => 'post', 'class' =>'form-horizontal','id'=>'validation-form', 'enctype' => 'multipart/form-data']) !!}

                                        @include($view_path.'.include.form')

                                        <div class="clearfix form-actions">
                                            <div class="col-md-offset-3 col-md-9">
                                                <button class="btn btn-info" type="submit">
                                                    <i class="ace-icon fa fa-check bigger-110"></i>
                                                    Submit
                                                </button>

                                                &nbsp; &nbsp; &nbsp;
                                                <button class="btn" type="reset">
                                                    <i class="ace-icon fa fa-undo bigger-110"></i>
                                                    Reset
                                                </button>
                                            </div>
                                        </div>
                                        <div class="hr hr-24"></div>
                                        <div class="space-24"></div>

                                    {!! Form::close() !!}

                                    <!-- PAGE CONTENT ENDS -->
                                </div><!-- /.col -->
                            </div><!-- /.row -->
                        </div>



                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->

@endsection

@section('js')

    <script src="{{ asset('admin_panel/assets/js/jquery.validate.min.js') }}"></script>
    <script src="{{ asset('admin_panel/assets/js/bootstrap-datepicker.min.js') }}"></script>


    <script>
        $(document).ready(function (){

            $('#validation-form').validate({
                rules: {
                    "title": {
                        required: true,
                        minlength: 5,
                    },
                    "image": {
                        required: true,
                    }
                },
            });

            $('.date-picker').datepicker({
                format: date('Y-m-d H:i:s'),
                autoclose: true,
                todayHighlight: true
            });


        });
    </script>

    @endsection

form.blade.php

<div class="form-group">

    {!! Form::label('title', 'Title', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


    <div class="col-sm-9">
        {!! Form::text('title', null, [
        'id' => 'title',
        'placeholder' => 'Title',
        'class' => 'col-xs-10 col-sm-5'
          ]) !!}

        @if($errors->has('title'))
            <span class="help-block error">
            <strong>{{ $errors->first('title') }}</strong>
            </span>
            @endif
    </div>
</div>

@if(isset($data['row']))

    <div class="form-group">

        {!! Form::label('existing_image', 'Existing Image', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


        <div class="col-sm-9">
            @if($data['row']->image)
                <img src="{{ asset('images/.$folder.'.$data['row']->image) }}" alt="" width="140">
              @else
                <p>No image</p>
            @endif
        </div>
    </div>

    @endif
<div class="form-group">
    {!! Form::label('main_image', 'Image', ['class' => 'col-sm-3 control-label no-padding-right']) !!}

    <div class="col-sm-9">

        {!! Form::file('main_image', [
        'id' => 'image',
        'class' => 'col-xs-10 col-sm-5'
        ]) !!}

        @if($errors->has('main_image'))
            <span class="help-block error">
            <strong>{{ $errors->first('main_image') }}</strong>
            </span>
        @endif
    </div>
</div>


<div class="form-group">

    {!! Form::label('writer', 'Writer', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


    <div class="col-sm-9">
        {!! Form::text('writer', null, [
        'id' => 'writer',
        'placeholder' => 'Enter Writer name',
        'class' => 'col-xs-10 col-sm-5'
          ]) !!}

        @if($errors->has('writer'))
            <span class="help-block error">
            <strong>{{ $errors->first('writer') }}</strong>
            </span>
        @endif
    </div>
</div>




<div class="form-group">

    {!! Form::label('short_des', 'Short Description', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


    <div class="col-sm-9">
        {!! Form::textarea('short_des', null, [
        'id' => 'short_des',
        'placeholder' => 'Enter a description here!',
        'class' => 'col-xs-10 col-sm-5'
          ]) !!}

        @if($errors->has('short_des'))
            <span class="help-block error">
            <strong>{{ $errors->first('short_des') }}</strong>
            </span>
        @endif
    </div>
</div>


<div class="form-group">

    {!! Form::label('detail_des', 'Detail Description', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


    <div class="col-sm-9">
        {!! Form::textarea('detail_des', null, [
        'id' => 'detail_des',
        'placeholder' => 'Enter a description here!',
        'class' => 'col-xs-10 col-sm-5'
          ]) !!}

        @if($errors->has('detail_des'))
            <span class="help-block error">
            <strong>{{ $errors->first('detail_des') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="space-4"></div>


<div class="form-group">

    {!! Form::label('publish_date', 'Publish Date', ['class' => 'col-sm-3 control-label no-padding-right']) !!}


    <div class="col-sm-9">
        {!! Form::text('publish_date', null, [
        'id' => 'publish_date',
        'placeholder' => 'YYYY/MM/DD',
        'class' => 'col-xs-10 col-sm-5 date-picker'
          ]) !!}

        @if($errors->has('publish_date'))
            <span class="help-block error">
            <strong>{{ $errors->first('publish_date') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group">
    {!! Form::label('status', 'Status', ['class' => 'col-sm-3 control-label no-padding-right' ]) !!}

    <div class="col-sm-9">
        <div class="control-group">
            <div class="radio">

                <label>
                    {!! Form::radio('status', 'active', true, ['class' => 'ace']) !!}
                    <span class="lbl">Active</span>
                </label>
            </div>
            <div class="radio">
                <label>
                    {!! Form::radio('status', 'in-active', false, ['class' => 'ace']) !!}
                    <span class="lbl">Inactive</span>
                </label>
            </div>
        </div>
    </div>
</div>

NewsController.php

<?php
/**
 * Created by PhpStorm.
 * User: ganes
 * Date: 05/08/2018
 * Time: 16:08
 */

namespace App\Http\Controllers\Admin;


use App\Http\Requests\Category\AddFormValidation;
use App\Http\Requests\Category\EditFormValidation;
use App\Models\News;
use Illuminate\Http\Request;

class NewsController extends AdminBaseController
{
    protected $base_route = 'admin.news';
    protected $view_path = 'admin.news';
    protected $folder_name = 'news';
    protected $panel = 'News';
    protected $folder_path;

    public function __construct()
    {
       $this->folder_path = public_path().DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$this->folder_name;
    }

    public function index()
    {

        $data = [];
        $data['rows'] = News::select('id', 'created_at', 'title','image','status')->get();


        return view(parent::loadDataToView($this->view_path.'.index'),compact('data'));
      }

    public function add()
    {
        return view(parent::loadDataToView($this->view_path.'.add'));
    }

    public function store(Request $request)
    {


          if($request->hasFile('main_image')){
              $image = $request -> file('main_image');
              $image_name = rand(1000, 5000).'_'.$image->getClientOriginalName();
              $image->move($this->folder_path, $image_name);
          }

        $request->request->add([
            'slug' => str_slug($request->get('title')),
            'status' => $request->get('status') == 'active'?1:0,
            'image' => isset($image_name)?$image_name:null
        ]);

        News::create($request->all());
        $request->session()->flash('success_message',$this->panel.'added successfully');
        return redirect()->route($this->base_route);
    }

    public function edit(Request $request, $id){
              $data = [];
              $data['row'] = Category::where('id', $id)->first();

        if(!$data['row']){
            $request->session()->flash('error_message','Invalid request.');
            return redirect()->route($this->base_route);
        }
        $data['row']->status = $data['row']->status == 1?'active':'in-active';
        return view(parent::loadDataToView($this->view_path.'.edit'),compact('data'));

    }

    public function update(EditFormValidation $request, $id)
    {
        $data=[];
        $data['row'] = Category::where('id',$id)->first();

        if($request->hasFile('main_image')){

            $image = $request -> file('main_image');
            $image_name = rand(1000, 5000).'_'.$image->getClientOriginalName();
            $image->move($this->folder_path, $image_name);

            if( $data['row']->image && file_exists($this->folder_path.DIRECTORY_SEPARATOR.$data['row']->image))
            unlink($this->folder_path.DIRECTORY_SEPARATOR.$data['row']->image);
        }

            $request->request->add([
                'slug' => str_slug($request->get('title')),
                'status' => $request->get('status') == 'active'?1:0,
                'image' => isset($image_name)?$image_name:$data['row']->image
            ]);
        $data['row']->update($request->all());
        $request->session()->flash('success_message','Category added successfully');
        return redirect()->route($this->base_route);
    }

    public function delete(Request $request, $id)
    {
        $data = [];
        $data['row'] = Category::where('id', $id)->first();

        if(!data['row']){
            $request->session()->flash('error_message','Invalid request.');
            return redirect()->route($this->base_route);
        }


        if( $data['row']->image && file_exists($this->folder_path.DIRECTORY_SEPARATOR.$data['row']->image))
            unlink($this->folder_path.DIRECTORY_SEPARATOR.$data['row']->image);

        $data['row']->delete();
        $request->session()->flash('success_message','Category deleted successfully');
        return redirect()->route($this->base_route);
    }

}

1 个答案:

答案 0 :(得分:0)

在插入为'Y-m-d H:i:s'格式时,整理publish_date列的日期时间格式,然后像这样发送:

insert into news (title, writer, short_des, detail_des,  publish_date, status, slug, image, updated_at, created_at) values ('Nepal Khabar', 'czczxc', 'zczzxzxczd', 'zczcdscsdcssd', '2018-08-22 00:00:00', 1, 'nepal-khabar', '1857_2.jpg', '2018-08-28 06:04:52', '2018-08-28 06:04:52'));

请确保发送包含在单引号中的日期。