Mariusz
Mariusz

Reputation: 131

Laravel 5.4 Calendar fetching start and end event from datebase

Hello I got some problems with fetching column 'start' and 'end' events to my calendar. I made loop and its working, title, date its showing on calendar but time doesnt work. All events is setting on allDay event but i set allDay false. I made create form and working good event make and sending to database. But this showing is really anoying. I trying fixed it but i dont know where i made mistake.

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\HomeModel;
use MaddHatter\LaravelFullcalendar\Event;
use Illuminate\Support\Facades\DB;
use DateTime;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
    

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){

                      

    $event = HomeModel::all();

    foreach ($event as $eve) {
      $events[] = \Calendar::event(
      $eve->title, //event title
      $eve->name,
      $eve->start, //start time (you can also use Carbon instead of DateTime)
      $eve->end, //end time (you can also use Carbon instead of DateTime)
      $eve->id //optionally, you can specify an event ID
      );
      //die($event);
    }
    $calendar = \Calendar::addEvents($events)
            ->setOptions([
                'FirstDay' => 1,
                'contentheight' => 650,
                'editable' => false,
                'allDay' => false,
                'aspectRatio' => 2,
                'slotLabelFormat' => 'HH:mm:ss',
                
                ])->setCallbacks([]);
            return view('home', compact('calendar'));

    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    
            public function create()
        {
          return view('create');
        }
        /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
           
       
        $time = explode(" - ", $request->input('daterange'));
                
        $event                  = new HomeModel;
        $event->name            = $request->input('name');
        $event->title           = $request->input('title');
        $event->start      = $time[0];
        $event->end        = $time[1];
        $event->save();
        
        $request->session()->flash('success', 'The event was successfully saved!');
        return redirect('home/create');
        


    }
}
Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class HomeModel extends Model
{
    protected $table = 'events'; // you may change this to your name table
	public $timestamps = true; // set true if you are using created_at and updated_at
	protected $primaryKey = 'id'; // the default is id
    
    
  
}

View:

@extends('layouts.app')

@section('content')

@endsection
@section('calendar')
<div class="panel panel-default" >
	<div class="panel-body">
	<a class="btn btn-default" href="{{ url('/home/create') }}">Create</a>
	<a class="btn btn-default" href="{{ url('/home/edit') }}">Edit</a>
	<a class="btn btn-default" href="{{ url('/home/delete') }}">Delete</a>
	</div>
	</div>
		<div class="panel-body">
							
							    {!! $calendar->calendar() !!}
							    {!! $calendar->script() !!}

 
									
		</div>						
</div>
				
@endsection

Screen how it look

Upvotes: 0

Views: 2418

Answers (1)

Nevermore
Nevermore

Reputation: 882

In case of date and time .. you might want to go through Carbon.

http://carbon.nesbot.com/docs/

It will help you to get the time and date in an easy manner and it will help you get rid of all the mumbo-jumbo that you are doing.

In your case:

foreach ($event as $eve) {

 $start_timestamp = Carbon::createFromFormat('d-m-Y h:i:s', $eve->start);
 $end_timestamp = Carbon::createFromFormat('d-m-Y h:i:s', $eve->end);

  $events[] = \Calendar::event(
  $eve->title, //event title
  $eve->name,
  $start_timestamp->toTimeString(), //start time (you can also use Carbon instead of DateTime)
  $end_timestamp->toTimeString(), //end time (you can also use Carbon instead of DateTime)
  $eve->id //optionally, you can specify an event ID
  );
  //die($event);
}

Upvotes: 0

Related Questions