PHP code example of dragon-code / last-modified

1. Go to this page and download the library: Download dragon-code/last-modified 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/ */

    

dragon-code / last-modified example snippets


use DragonCode\LastModified\Middlewares\CheckLastModified;

protected $middlewareGroups = [
    'web' => [
         CheckLastModified::class,
    ]
]

use Carbon\Carbon;
use DragonCode\LastModified\Resources\Item;
use DragonCode\LastModified\Services\LastModified;

public function handle()
{
    $collection_1 = Foo::whereIsActive(true)->get();
    $collection_2 = Bar::where('id', '>', 50)->get();
    $collection_3 = Baz::query()->get();

    $builder_1 = Foo::whereIsActive(true);
    $builder_2 = Bar::where('id', '>', 50);
    $builder_3 = Baz::query();
    
    $model_1 = Foo::whereIsActive(true)->first();
    $model_2 = Bar::where('id', '>', 50)->first();
    $model_3 = Baz::query()->first();
    
    $item_1 = Item::make(['url' => 'https://example.com/foo',      'updated_at' => Carbon::now());
    $item_2 = Item::make(['url' => 'https://example.com/bar',      'updated_at' => Carbon::parse('2018-03-02'));
    $item_3 = Item::make(['url' => 'https://example.com/baz?id=1', 'updated_at' => Carbon::now());
    
    LastModified::make()
        ->collections($collection_1, $collection_2, $collection_3)
        ->builders($builder_1, $builder_2, $builder_3)
        ->models($model_1, $model_2, $model_3)
        ->manual($item_1, $item_2, $item_3)
        ->update();
}

protected getUrlAttribute(): string
{
    $slug = $this->slug;

    return route('page.show', compact('slug'));
}

use Carbon\Carbon;
use DragonCode\LastModified\Resources\Item;
use DragonCode\LastModified\Services\LastModified;

public function handle()
{    
    $collection_1 = Foo::whereIsActive(false)->get();
    $collection_2 = Bar::whereIn('id', [50, 60, 62, 73])->get();

    $builder_1 = Foo::whereIsActive(true);
    $builder_2 = Bar::where('id', '>', 50);
    $builder_3 = Baz::query();
    
    $model_1 = Foo::whereIsActive(false)->first();
    $model_2 = Bar::whereIn('id', [50, 60, 62, 73])->first();
    
    $item_1 = Item::make(['url' => 'https://example.com/foo', 'updated_at' => Carbon::now()]);
    $item_2 = Item::make(['url' => 'https://example.com/bar', 'updated_at' => Carbon::now()]);
    
    LastModified::make()
        ->collections($collection_1, $collection_2, $collection_3)
        ->builders($builder_1, $builder_2, $builder_3)
        ->models($model_1, $model_2, $model_3)
        ->manual($item_1, $item_2, $item_3)
        ->delete();
}

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;
use DragonCode\LastModified\Services\LastModified;

class LastModifiedObserver
{
    public function saving(Model $model)
    {
        LastModified::make()
            ->models($model)
            ->update();
    }

    public function deleting(Model $model)
    {
        LastModified::make()
            ->models($model)
            ->delete();
    }
}

namespace App\Providers;

use App\Observers\LastModifiedObserver;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\ServiceProvider;

class ObserverServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Page::observe(LastModifiedObserver::class);
        News::observe(LastModifiedObserver::class);

        // ...
    }
}