使用daterange时如何存储开始时间和结束时间?

时间:2019-10-07 09:05:17

标签: php laravel

我正在使用fullcalendar来显示数据库中的事件,问题是当我添加新事件时,似乎只有开始日期和结束日期存储在数据库中。我的代码有什么问题?

EventController.php


public function store(Request $request)
    {


     $this->validate($request, [
        'event_name' => 'required|string|min:2|max:255',
        'daterange' => 'required',
    ]);
     $time_start = $request->input('start_time');

     $time_end = $request->input('end_time');

     $tempDate = $request->input('daterange');
     $temp2 = str_replace('-', null, $tempDate);
     $temp2 = str_replace('/', '-', $temp2);
     $temp3 = explode('  ', $temp2);

     $date1 = date_create_from_format('m-d-Y', $temp3[0]);
     $date2 = date_create_from_format('m-d-Y', $temp3[1]);





    $event = new event([
        'event_name' => $request->input('event_name'),
        'event_desc' => $request->input('event_desc'),
        'event_venue' => $request->input('event_venue'),
        'event_start' => date_format($date1, 'Y-m-d'),
        'event_finish' => date_format($date2, 'Y-m-d'),

        'event_dow' => $event_dow, //days of week
        'evcat_id' => $request->input('evcat_id'),
        'user_id' => $request->input('user_id')


        ]);

    $event->save();
    Session::flash('success', 'Event Created Successfully');
    return redirect()->route('event.sched');

}

我的html文件

              <div class="row">
                    <div class="col-md-8">
                        <label class="control-label">Start Date - End Date</label>
                        <input type="text" onclick = "getTime()" class="form-control input-daterange-timepicker" id = 'daterange' name="daterange" value="" />
                        <input type="text" id="start_time">
                        <input type="text" id="end_time">
                    </div>

                </div>

我的路线(web.php)


Route::resource('EventCategory', 'EventCategoryController');
Route::resource('Event', 'EventController');

Route::get('/sched', 'EventController@create')->name('event.sched');
Route::post('/sched/add', 'EventController@store')->name('event.add');
Route::post('/sched/edit/{id}','EventCategoryController@update')->name('event.edit');

1 个答案:

答案 0 :(得分:0)

EventController.php

 public function store(Request $request)
{
    //Validating title and body field
    $this->validate($request, [
        'title'=>'required',
        'body'=>'required',
        'color' =>'required',
        'start_date' =>'required',
        'end_date' =>'required',

    ]);
    $event = new Event;
    $event->title = $request->title;
    $event->body = $request->body;
    $event->color = $request->color;
    $event->start_date = $request->start_date;
    $event->end_date = $request->end_date;
    $event->save();
    return redirect()->route('events.index');
}


 public function index()
 {
    $eventCount = Event::count();
    $events = Event::latest()->paginate(8);
    $event = [];

    foreach ($events as $row){
        $end_date = $row->end_date. "24:00:00";
        $event[] = Calendar::event(
            $row->title,
            true,
            new \DateTime($row->start_date),
            new \DateTime($row->end_date),
            $row->id,
            [
                'color' => $row->color,
                'url' => 'events',
            ]
        );
    }
    $calendar = \Calendar::addEvents($event);
    return view('home', compact('events','calendar','eventCount'));
   }

在您的刀片文件中

<script>
    $(document).ready(function() {
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay',
            },
            lang: 'fa',
            isJalaali: true,
            isRTL: true,
            businessHours: true, // display business hours
            editable: true,
            events : [
                    @foreach($events as $event)
                {
                    title : '{{ $event->title }}',
                    color : '{{ $event->color }}',
                    start : '{{ $event->start_date }}',
                    @if ($event->end_date)
                    end: '{{ $event->end_date }}',
                    @endif
                },
                @endforeach
            ],
            eventClick: function(calEvent, jsEvent, view) {
                $('#start_date').val(moment(calEvent.start).format('YYYY-MM-DD HH:mm:ss'));
                $('#end_date').val(moment(calEvent.end).format('YYYY-MM-DD HH:mm:ss'));
                $('#editModal').modal();
            }
        });
    });
</script>
{!! $calendar->script() !!}