En este tutorial te mostraremos cómo crear un calendario de eventos con Laravel y MySQL.

Si no viste la primera parte debes ver el tutorial anterior
https://www.tutofox.com/php/laravel/tutorial-laravel-calendario-crear-una-vista-de-calendario/
Crear un modelo de Evento
php artisan make:model Event
app/Event.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $table= 'evento';
//
protected $fillable = [
'titulo', 'descripcion', 'fecha',
];
public $timestamps = false;
}
Crear una tabla para base de datos en SQL
CREATE TABLE `evento` (
`id` int(11) NOT NULL,
`titulo` int(11) NOT NULL,
`descripcion` int(11) NOT NULL,
`fecha` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_spanish2_ci;
-
Crear un controlador de Evento
php artisan make:controller ControllerEvent
Agregar una funcion para vista el formulario de evento
public function form(){
return view("evento/form");
}
Crear una vista de formulario de evento
resources/views/evento/form.blade.php
<html>
<head>
<title></title>
<meta content="">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Exo&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
body{
font-family: 'Exo', sans-serif;
}
.header-col{
background: #E3E9E5;
color:#536170;
text-align: center;
font-size: 20px;
font-weight: bold;
}
.header-calendar{
background: #EE192D;color:white;
}
.box-day{
border:1px solid #E3E9E5;
height:150px;
}
.box-dayoff{
border:1px solid #E3E9E5;
height:150px;
background-color: #ccd1ce;
}
</style>
</head>
<body>
<div class="container">
<div style="height:50px"></div>
<h1>< tutofox /> <small>Oh my code!</small></h1>
<p class="lead">
<h3>Evento</h3>
<p>Formulario de evento</p>
<a class="btn btn-default" href="{{ asset('/Evento/index') }}">Atras</a>
<hr>
@if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif
<div class="col-md-6">
<form action="{{ asset('/Evento/create/') }}" method="post">
@csrf
<div class="fomr-group">
<label>Titulo</label>
<input type="text" class="form-control" name="titulo">
</div>
<div class="fomr-group">
<label>Descripcion del Evento</label>
<input type="text" class="form-control" name="descripcion">
</div>
<div class="fomr-group">
<label>Fecha</label>
<input type="date" class="form-control" name="fecha">
</div>
<br>
<input type="submit" class="btn btn-info" value="Guardar">
</form>
</div>
<!-- inicio de semana -->
</div> <!-- /container -->
<!-- Footer -->
<footer class="page-footer font-small blue pt-4">
<!-- Copyright -->
<div class="footer-copyright text-center py-3">
Developed by Artyom from
<a href="https://www.tutofox.com/"> < tutofox/></a>
</div>
<!-- Copyright -->
</footer>
<!-- Footer -->
</body>
</html>
Crear una vista de detalles de Evento
resources/views/evento/evento.blade.php
<html>
<head>
<title></title>
<meta content="">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Exo&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
body{
font-family: 'Exo', sans-serif;
}
.header-col{
background: #E3E9E5;
color:#536170;
text-align: center;
font-size: 20px;
font-weight: bold;
}
.header-calendar{
background: #EE192D;color:white;
}
.box-day{
border:1px solid #E3E9E5;
height:150px;
}
.box-dayoff{
border:1px solid #E3E9E5;
height:150px;
background-color: #ccd1ce;
}
</style>
</head>
<body>
<div class="container">
<div style="height:50px"></div>
<h1>< tutofox /> <small>Oh my code!</small></h1>
<p class="lead">
<h3>Evento</h3>
<p>Detalles de evento</p>
<a class="btn btn-default" href="{{ asset('/Evento/index') }}">Atras</a>
<hr>
<div class="col-md-6">
<form action="{{ asset('/Evento/create/') }}" method="post">
<div class="fomr-group">
<h4>Titulo</h4>
{{ $event->titulo }}
</div>
<div class="fomr-group">
<h4>Descripcion del Evento</h4>
{{ $event->descripcion }}
</div>
<div class="fomr-group">
<h4>Fecha</h4>
{{ $event->fecha }}
</div>
<br>
<input type="submit" class="btn btn-info" value="Guardar">
</form>
</div>
<!-- inicio de semana -->
</div> <!-- /container -->
<!-- Footer -->
<footer class="page-footer font-small blue pt-4">
<!-- Copyright -->
<div class="footer-copyright text-center py-3">
Developed by Artyom from
<a href="https://www.tutofox.com/"> < tutofox/></a>
</div>
<!-- Copyright -->
</footer>
<!-- Footer -->
</body>
</html>
Crear una vista de Calendario con Eventos
resources/views/evento/calendario.blade.php
<html>
<head>
<title></title>
<meta content="">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Exo&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
body{
font-family: 'Exo', sans-serif;
}
.header-col{
background: #E3E9E5;
color:#536170;
text-align: center;
font-size: 20px;
font-weight: bold;
}
.header-calendar{
background: #EE192D;color:white;
}
.box-day{
border:1px solid #E3E9E5;
height:150px;
}
.box-dayoff{
border:1px solid #E3E9E5;
height:150px;
background-color: #ccd1ce;
}
</style>
</head>
<body>
<div class="container">
<div style="height:50px"></div>
<h1>< tutofox /> <small>Oh my code!</small></h1>
<p class="lead">
<h3>Calendario - evento</h3>
<a class="btn btn-default" href="{{ asset('/Evento/form') }}">Crear un evento</a>
<hr>
<div class="row header-calendar" >
<div class="col" style="display: flex; justify-content: space-between; padding: 10px;">
<a href="{{ asset('/Calendar/event/') }}/<?= $data['last']; ?>" style="margin:10px;">
<i class="fas fa-chevron-circle-left" style="font-size:30px;color:white;"></i>
</a>
<h2 style="font-weight:bold;margin:10px;"><?= $mespanish; ?> <small><?= $data['year']; ?></small></h2>
<a href="{{ asset('/Calendar/event/') }}/<?= $data['next']; ?>" style="margin:10px;">
<i class="fas fa-chevron-circle-right" style="font-size:30px;color:white;"></i>
</a>
</div>
</div>
<div class="row">
<div class="col header-col">Lunes</div>
<div class="col header-col">Martes</div>
<div class="col header-col">Miercoles</div>
<div class="col header-col">Jueves</div>
<div class="col header-col">Viernes</div>
<div class="col header-col">Sabado</div>
<div class="col header-col">Domingo</div>
</div>
<!-- inicio de semana -->
@foreach ($data['calendar'] as $weekdata)
<div class="row">
<!-- ciclo de dia por semana -->
@foreach ($weekdata['datos'] as $dayweek)
@if ($dayweek['mes']==$mes)
<div class="col box-day">
{{ $dayweek['dia'] }}
<!-- evento -->
@foreach ($dayweek['evento'] as $event)
<a class="badge badge-primary" href="{{ asset('/Evento/details/') }}/{{ $event->id }}">
{{ $event->titulo }}
</a>
@endforeach
</div>
@else
<div class="col box-dayoff">
</div>
@endif
@endforeach
</div>
@endforeach
</div> <!-- /container -->
<!-- Footer -->
<footer class="page-footer font-small blue pt-4">
<!-- Copyright -->
<div class="footer-copyright text-center py-3">
Developed by Artyom from
<a href="https://www.tutofox.com/"> < tutofox/></a>
</div>
<!-- Copyright -->
</footer>
<!-- Footer -->
</body>
</html>
Configurar route —-> routes/web.php
Route::get('Evento/form','ControllerEvent@form');
Route::post('Evento/create','ControllerEvent@create');
Route::get('Evento/details/{id}','ControllerEvent@details');
Route::get('Evento/index','ControllerEvent@index');
Route::get('Evento/index/{month}','ControllerEvent@index_month');
Route::post('Evento/calendario','ControllerEvent@calendario');
Controlador completo
app/Http/Controllers/ControllerEvent.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Event;
class ControllerEvent extends Controller
{
//
public function form(){
return view("evento/form");
}
public function create(Request $request){
$this->validate($request, [
'titulo' => 'required',
'descripcion' => 'required',
'fecha' => 'required'
]);
Event::insert([
'titulo' => $request->input("titulo"),
'descripcion' => $request->input("descripcion"),
'fecha' => $request->input("fecha")
]);
return back()->with('success', 'Enviado exitosamente!');
}
public function details($id){
$event = Event::find($id);
return view("evento/evento",[
"event" => $event
]);
}
// =================== CALENDARIO =====================
public function index(){
$month = date("Y-m");
//
$data = $this->calendar_month($month);
$mes = $data['month'];
// obtener mes en espanol
$mespanish = $this->spanish_month($mes);
$mes = $data['month'];
return view("evento/calendario",[
'data' => $data,
'mes' => $mes,
'mespanish' => $mespanish
]);
}
public function index_month($month){
$data = $this->calendar_month($month);
$mes = $data['month'];
// obtener mes en espanol
$mespanish = $this->spanish_month($mes);
$mes = $data['month'];
return view("evento/calendario",[
'data' => $data,
'mes' => $mes,
'mespanish' => $mespanish
]);
}
public static function calendar_month($month){
//$mes = date("Y-m");
$mes = $month;
//sacar el ultimo de dia del mes
$daylast = date("Y-m-d", strtotime("last day of ".$mes));
//sacar el dia de dia del mes
$fecha = date("Y-m-d", strtotime("first day of ".$mes));
$daysmonth = date("d", strtotime($fecha));
$montmonth = date("m", strtotime($fecha));
$yearmonth = date("Y", strtotime($fecha));
// sacar el lunes de la primera semana
$nuevaFecha = mktime(0,0,0,$montmonth,$daysmonth,$yearmonth);
$diaDeLaSemana = date("w", $nuevaFecha);
$nuevaFecha = $nuevaFecha - ($diaDeLaSemana*24*3600); //Restar los segundos totales de los dias transcurridos de la semana
$dateini = date ("Y-m-d",$nuevaFecha);
//$dateini = date("Y-m-d",strtotime($dateini."+ 1 day"));
// numero de primer semana del mes
$semana1 = date("W",strtotime($fecha));
// numero de ultima semana del mes
$semana2 = date("W",strtotime($daylast));
// semana todal del mes
// en caso si es diciembre
if (date("m", strtotime($mes))==12) {
$semana = 5;
}
else {
$semana = ($semana2-$semana1)+1;
}
// semana todal del mes
$datafecha = $dateini;
$calendario = array();
$iweek = 0;
while ($iweek < $semana):
$iweek++;
//echo "Semana $iweek <br>";
//
$weekdata = [];
for ($iday=0; $iday < 7 ; $iday++){
// code...
$datafecha = date("Y-m-d",strtotime($datafecha."+ 1 day"));
$datanew['mes'] = date("M", strtotime($datafecha));
$datanew['dia'] = date("d", strtotime($datafecha));
$datanew['fecha'] = $datafecha;
//AGREGAR CONSULTAS EVENTO
$datanew['evento'] = Event::where("fecha",$datafecha)->get();
array_push($weekdata,$datanew);
}
$dataweek['semana'] = $iweek;
$dataweek['datos'] = $weekdata;
//$datafecha['horario'] = $datahorario;
array_push($calendario,$dataweek);
endwhile;
$nextmonth = date("Y-M",strtotime($mes."+ 1 month"));
$lastmonth = date("Y-M",strtotime($mes."- 1 month"));
$month = date("M",strtotime($mes));
$yearmonth = date("Y",strtotime($mes));
//$month = date("M",strtotime("2019-03"));
$data = array(
'next' => $nextmonth,
'month'=> $month,
'year' => $yearmonth,
'last' => $lastmonth,
'calendar' => $calendario,
);
return $data;
}
public static function spanish_month($month)
{
$mes = $month;
if ($month=="Jan") {
$mes = "Enero";
}
elseif ($month=="Feb") {
$mes = "Febrero";
}
elseif ($month=="Mar") {
$mes = "Marzo";
}
elseif ($month=="Apr") {
$mes = "Abril";
}
elseif ($month=="May") {
$mes = "Mayo";
}
elseif ($month=="Jun") {
$mes = "Junio";
}
elseif ($month=="Jul") {
$mes = "Julio";
}
elseif ($month=="Aug") {
$mes = "Agosto";
}
elseif ($month=="Sep") {
$mes = "Septiembre";
}
elseif ($month=="Oct") {
$mes = "Octubre";
}
elseif ($month=="Oct") {
$mes = "December";
}
elseif ($month=="Dec") {
$mes = "Diciembre";
}
else {
$mes = $month;
}
return $mes;
}
}

Código fuentes
Gracias amigos por tus buenos tutoriales. me ha servido de mucho gracias por tu tiempo y conocimiento.
Buenos días amigo una pregunta este proyecto no tiene conexión a base de datos?
Amigo muchas gracias por compartir el conocimiento, lo implemente todo el proyecto y me salio super bien todo.
hola, muy buen aporte, consulta han resuelto el problema de los meses que comienzan en domingo, lo cuales se muestra el dia 2 y se corta el primer dia?