This Laravel package provides extended functions for Eloquent.
-
Begin by installing this package through Composer. Edit your project's
composer.json
file to requirepaulvl/magicmodel
."require-dev": { "paulvl/magicmodel": "dev-master" }
There is no support for Laravel 5.
-
Next, update Composer from the Terminal:
composer update --dev
-
Once this operation completes, add the service provider. Open
app/config/app.php
, and add a new item to the providers array.'PaulVL\MagicModel\MagicModelServiceProvider'
-
And add a new item to the aliases array, on same
app/config/app.php
file.'MagicModel' => 'PaulVL\MagicModel\MagicModel'
-
Finally in order to use MagicModel properly you have to extend your "Model" from MagicModel instead of Eloquent like this for example:
<?php . . . class User extends MagicModel implements UserInterface, RemindableInterface { . . .
##Usage
MagicModel implements the following methods:
MagicModel allows you to easily verify if a record is referenced by another one as a foreing key. You can use the static Model::hasReferences($id)
method directly from your MagicModel's extended Model:
example...
<?php
return dd(Model::hasReferences(1));
//returns True if primary key "1" is referenced in any table as FK.
//returns False if primary key "1" is NOT referenced in any table as FK.
?>
Or you can either use $object->isReferenced()
method from an instanced object:
example...
<?php
$object = Model::find(1);
return dd($object->isReferenced());
//returns True if the object is referenced in any table as FK.
//returns False if the object is NOT referenced in any table as FK.
?>