Blog Detail

15

Sep
Easily Get Information about the Models in your Laravel App cover image

arrow_back Easily Get Information about the Models in your Laravel App

Laravel Model info is an excellent package by Spatie. This package offers a way to get information about the models in your Laravel app. Using this package you can determine which attributes and relations your model classes have.

Installation

You can install the package via composer:

composer require spatie/laravel-model-info

Usage

You can get information about a model by calling forModel:

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

Getting a specific attribute

use Spatie\ModelInfo\ModelInfo;

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

Getting a specific relation

You can get a specific relation using the relation method.

use Spatie\ModelInfo\ModelInfo;

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

Discovering all models in your application

Using this method we’ll discover all methods in your project, no matter in which directory they are stored.

use Spatie\ModelInfo\ModelFinder;

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

Getting information on all models in your application

The ModelInfo class can get information about all models in your application.

use Spatie\ModelInfo\ModelInfo;

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

Adding extra info on a model

To add extra info on a model, add a method extraModelInfo to your model. It can return anything you want: an string, an object, an array.

// in your model

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

The returned value will be available on the extra property of a ModelInfo instance.

use Spatie\ModelInfo\ModelInfo;

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

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

For more details, please visit Github.

In Closing

If you are looking for a custom CRM system or eCommerce web application for your enterprise, then we are here to help you. The Web development team at Codebrisk has notable experience in the Laravel e-commerce and CRM web development field. We can provide you with custom-made e-commerce web applications with a lot of cutting-edge features and options. We will be pleased to assist you with your projects. Please feel free to contact us or you can launch a project with us.

Published at : 15-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project