Giter Site home page Giter Site logo

spatie / laravel-model-info Goto Github PK

View Code? Open in Web Editor NEW
165.0 2.0 18.0 140 KB

Get information on all the models in your Laravel project

Home Page: https://freek.dev/2332-getting-information-about-all-the-models-in-your-laravel-app

License: MIT License

PHP 100.00%
eloquent laravel models php reflection

laravel-model-info's Introduction

Get information about the models in your Laravel app

Latest Version on Packagist Total Downloads

Using this package you can determine which attributes and relations your model classes have.

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

Here's how you can get information about the attributes:

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

Here's how you can get information about the relations

// 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; 

Additionally, the package can also discover all the models in your application.

use Spatie\ModelInfo\ModelFinder;

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

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

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

Note

This package discovers relationships by their return type. Make sure that, in all your models each method that returns a relation has a return type.

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')

Attributes

A Spatie\ModelInfo\Attributes\Attribute object has these properties:

  • name
  • type
  • increments
  • nullable
  • default
  • unique
  • fillable
  • appended
  • cast
  • virtual

Relationships

A Spatie\ModelInfo\Relations\Relation object has these properties:

  • name
  • type
  • related

It also has a relatedModelInfo() method that gives a ModelInfo instance for the related model.

use Spatie\ModelInfo\ModelInfo;

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

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 model 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'

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This package contains code taken from the model:show command of Laravel.

License

The MIT License (MIT). Please see License File for more information.

laravel-model-info's People

Contributors

dependabot[bot] avatar freekmurze avatar github-actions[bot] avatar hafezdivandari avatar idevelopthings avatar laravel-shift avatar mertcandinler avatar patrickcurl avatar patrickweh avatar pikant avatar riley19280 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.