While working with Laravel, sometimes you have to return fields that are not in your db table (https://laravel.com/docs/8.x/eloquent-serialization#appending-values-to-json). However, you may want to hide these fields that you add in some cases. In this case, you need to dominate the eloquent model.Now, you can do it easily with this package.
This package includes a Appends
trait. You can use the Appends
trait in any model you want. For example, if you want
to add to the User model:
class User extends Model
{
use HasFactory, Appends; // <--add this;
...
You can install the package via composer for Laravel 5.6+:
composer require soysaltan/appender
class User extends Model
{
use HasFactory, Appends; // <--add this;
protected $appends = ['created_at_by_human', 'created_at_formatted'];
If you want to get your model without appends, just drop this line
User::$withoutAppends=true; // User -> your user mmodel
Note: If your model extends a custom class, you need to ensure that
your extended class has Concerns\HasAttributes
trait.Otherwise this package doesn't work since
it uses Concerns\HasAttributes::getArrayableAppends
method
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.