PHP code example of greenimp / laravel-date-range

1. Go to this page and download the library: Download greenimp/laravel-date-range library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

greenimp / laravel-date-range example snippets



namespace App\Models;

use GreenImp\DateRange\Contracts\HasDateRange;
use GreenImp\DateRange\DateRangeOptions;
use GreenImp\DateRange\InteractsWithDateRange;
use Illuminate\Database\Eloquent\Model;

class SingleDateModel extends Model implements HasDateRange
{
    use InteractsWithDateRange;
    
    /**
     * Get the options for the date range.
     *
     * @return DateRangeOptions
     */
    public function getDateRangeOptions(): DateRangeOptions
    {
        return DateRangeOptions::create()
            // these are optional - if not called, the default values from the config will be used
            ->startAtField('from')
            ->endAtField('to');
    }
}



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

return new class extends Migration {
    public function up()
    {
        Schema::create('your_eloquent_models', function (Blueprint $table) {
            $table->id();
            $table->datetime('from'); // Field name same as the `startAtField` in your `getDateRangeOptions`
            $table->datetime('to'); // Field name same as the `endAtField` in your `getDateRangeOptions`
            $table->timestamps();
        });
    }
}


namespace App\Models;

use GreenImp\DateRange\Contracts\HasDateRanges;
use GreenImp\DateRange\InteractsWithDateRanges;
use Illuminate\Database\Eloquent\Model;

class MultipleDateModel extends Model implements HasDateRanges
{
    use InteractsWithDateRanges;
}



return [
    'models' => [
        // Set the value to your custom class.
        'date_range' => \GreenImp\DateRange\Models\DateRange::class,
    ],
    ...
];



return [
    ...
    'table_names' => [
        // Set the value to your custom table name.
        'date_ranges' => 'date_ranges',
    ],
    ...
];
bash
php artisan vendor:publish --provider="GreenImp\DateRange\DateRangeServiceProvider" --tag="migrations"
bash
> php artisan config:clear
> 
bash
php artisan migrate
bash
php artisan vendor:publish --provider="GreenImp\DateRange\DateRangeServiceProvider" --tag="config"