PHP code example of malsanyang / laravel-model-tools

1. Go to this page and download the library: Download malsanyang/laravel-model-tools 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/ */

    

malsanyang / laravel-model-tools example snippets


use Illuminate\Database\Eloquent\Model;
use MalSanyang\ModelTools\Concerns\HasReference;

class Page extends Model
{
    use HasReference;
}

$table->uuid('reference')->nullable()->unique()->after('id');

$page = new Page;
$page->title = 'About us';
$page->save();

$page->reference;

$page = Page::reference($uuid);

$page = Page::referenceOrFail($uuid);

return [
    'reference_column' => 'reference',
    'use_reference_as_route_key' => true,
];

'reference_column' => 'uuid',

class Page extends Model
{
    use HasReference;

    public static function referenceColumn(): string
    {
        return 'uuid';
    }

    public function getRouteKeyName(): string
    {
        return config('model-tools.use_reference_as_route_key')
            ? static::referenceColumn()
            : parent::getRouteKeyName();
    }
}

Route::get('/pages/{page}', function (Page $page) {
    return $page;
});

'use_reference_as_route_key' => false,

use MalSanyang\ModelTools\Models\BaseModel;

class Page extends BaseModel
{
    // Includes HasReference, SoftDeletes, and datetime casts.
}
bash
php artisan vendor:publish --tag=model-tools-config
bash
php artisan model-tools:reference-migration pages
php artisan migrate
bash
php artisan model-tools:reference-migration pages --column=uuid