jeudi 16 mai 2019

Static properties on a Laravel model class

I'm trying to create a standard index() method for most of my controllers that combines search, pagination, and sorting into one retrieve all listings functionality. I'm using a trait for those controllers which contains an index method, and that method uses some static properties defined on the associated model to know which columns to search and so on. For example, my User.php contains:

   /**
     * @var array
     */
    public static $searchableColumns = [
        'name',
        'username',
        'title',
        'email'
    ];

    /**
     * @var array
     */
    public static $searchableRelations = [
        'groups' => ['searchOn' => 'name']
    ];

    /**
     * @var string
     */
    public static $defaultOrderBy = 'name';

My issue is that...this just doesn't feel right. Every other property on the class is a protected instance variable, e..g:

protected $fillable = [
        'username',
        'email',
        ...
];

Is declaring static properties on a Model bad practice? If so, where should I define something like this?

Aucun commentaire:

Enregistrer un commentaire