Hi guys,
We all know that Laravel's Eloquent is using PHP's magic methods (__get
& __set
) for the columns, accessors, relationships,... and much more.
When the development happens, it hards for us to know which "properties" that we can use for a particular Eloquent models. We have to remember the columns' names, the relationship names,... It is a big pain, IKR?
An alternative that we got - is using the phpDoc block and add the properties, eg:
/**
* @property-read int $id // column
* @property string $name // column
* @property-read Country|null $country // relationship
* ...
*/
class User extends Model
{
With this way, it is super IDE-friendly (eg PHPStorm) and increase your productivity a lot.
Butttt, doing the manually work is not so fun, is it? So here am I with the EloquentDocs generator.
Repo: https://github.com/sethsandaru/eloquent-docs
Installation
Simply hit this to install, dev only:
composer require sethphat/eloquent-docs --dev
Laravel auto-discovery will automatically do the magic 😉
Usage
View properties of your model
php artisan eloquent:phpdoc App\Models\User
It will show up all the properties (columns, relationships,...) for you to preview, before performing the write
Write properties to your model
php artisan eloquent:phpdoc App\Models\User --write
It will write the phpDoc block to your model (overwrite if your model already has a phpDoc block)
Optionally, use short class names
php artisan eloquent:phpdoc App\Models\User --short-class
From * @property-read App\Models\Country|null $country
to * @property-read Country|null $country
Next Goals
- Ability to scan a whole folders and replace the phpDoc blocks automatically.
- Add more helper/suggestion methods for Eloquent models
Thanks guys, have a great productive works with Laravel!
Top comments (0)