PHP code example of spatie / laravel-model-info

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

    

spatie / laravel-model-info example snippets


use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->fileName; // returns the filename that contains your model
$modelInfo->tableName; // returns the name of the table your models are stored in
$modelInfo->attributes; // returns a collection of `Attribute` objects
$modelInfo->relations; // returns a collection of `Relation` objects

$modelInfo->attributes->first()->name; // returns the name of the first attribute
$modelInfo->attributes->first()->type; // returns the type of the first attribute (string, integer, ...)

// returns the name of the first relation, eg. `author`
$modelInfo->relations->first()->name;

// returns the type of the
// first relation, eg. `BelongsTo`
$modelInfo->relations->first()->type;

// returns the related model of the
// first relation, eg. `App\Models\User`
$modelInfo->relations->first()->related; 

use Spatie\ModelInfo\ModelFinder;

// returns a `Illuminate\Support\Collection` containing all
// the class names of all your models.
$models = ModelFinder::all(); 

use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->fileName; // returns the filename that contains your model
$modelInfo->tableName; // returns the name of the table your models are stored in
$modelInfo->attributes; // returns a collection of `Attribute` objects
$modelInfo->relations; // returns a collection of `Relation` objects

use Spatie\ModelInfo\ModelInfo;

// returns an instance of `Spatie\ModelInfo\Attributes\Attribute`
ModelInfo::forModel(BlogPost::class)->attribute('name');

use Spatie\ModelInfo\ModelInfo;

// returns an instance of `Spatie\ModelInfo\Relations\Relation`
ModelInfo::forModel(BlogPost::class)->relation('user')

use Spatie\ModelInfo\ModelInfo;

ModelInfo::forModel(BlogPost::class)
   ->relation('user')
   ->relatedModelInfo() // returns the `ModelInfo` for the `User` model

use Spatie\ModelInfo\ModelFinder;

// returns a `Illuminate\Support\Collection` containing
// all the class names of all your models.
$models = ModelFinder::all(); 

use Spatie\ModelInfo\ModelInfo;

ModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances

// in your model

public function extraModelInfo()
{
    return 'anything you want';
}

use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->extra; // returns 'anything you want'