Worked on a small project that was about a website that must render a calendar for dynamic years, and point out of its a vacation, and some type of text for it.

This is one of the pages ( the homepage ), which loops thru all months in the given active year.

The empty card is for Ads, and its for a local project, so there is a cyrilic involved in the days.

So how we ended up here, and lets see how the code looks like

 

Years

Lets take a look at the Year Model in App , nothing serious or strange:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Year extends Model
{
    protected $guarded = [];
    public function months() {
        return $this->hasMany('App\Month','year_id');
    }


    public function slug() {
        $slug = url('g/'.$this->slug);

        return $slug;
    }

    public $casts = [
        'date_year' => 'date',
    ];

}

And the Migration File :

 

 public function up()
    {
        Schema::create('years', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            $table->string('str_name');
            $table->unsignedInteger('year_integer');
            $table->string('slug');
            $table->boolean('is_visible_nav')->default(true);
            $table->date('date_year')->nullable();
            $table->string('seo_title')->nullable();
            $table->string('seo_description')->nullable();
            $table->longText('body')->nullable();

        });
    }

 

Some people might think why is that even needed, when we can render everything dynamic with Carbon, and not involving a single query. That is correct, but its better to have these custom meta texts for google and for SEO, which will allow you to edit specifically the metas and text body for different years.

 

Months

Model:

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Month extends Model
{
    protected $guarded = [];


    public function year() {
        return $this->belongsTo('App\Year','year_id');
    }

    public function days() {
        return $this->hasMany('App\Day','month_id');
    }

    public $casts = [
        'date_month' => 'date',
    ];

    public function urlslug() {
        $slug = url( 'kalendar/' .$this->slug);

        return $slug;
    }

}

And the migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCalendarMonthsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('months', function (Blueprint $table) {
            $table->id();
            $table->string('str_name');
            $table->unsignedInteger('month_integer');
            $table->unsignedInteger('firstday');
            $table->unsignedInteger('lastday');
            $table->unsignedInteger('totaldays');
            $table->string('slug');

            $table->date('date_month')->nullable();
            $table->string('seo_title')->nullable();
            $table->string('seo_description')->nullable();
            $table->longText('body')->nullable();
            $table->timestamps();

            $table->foreignId('year_id')->nullable();
            $table->foreign('year_id')
                ->references('id')->on('years')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('calendar_months');
    }
}