Utilities for model Eloquent Laravel
<?php | |
namespace App; | |
/** | |
* Class ModelUtils | |
* Methods and attributes commonly used by application of models. | |
* | |
* @package App | |
*/ | |
trait ModelUtils | |
{ | |
protected $columnActive = 'ativo'; | |
public function enable() | |
{ | |
$this->update([$this->columnActive => true]); | |
} | |
public function disable() | |
{ | |
$this->update([$this->columnActive => false]); | |
} | |
/** | |
* Search for a record by the 'column'. | |
* | |
* @param string $column | |
* @param string $value | |
* @param array $columns | |
* @return static | |
*/ | |
public static function findBy(string $column, string $value, array $columns = ['*']) | |
{ | |
return static::query() | |
->withTrashed() | |
->where($column, $value) | |
->get($columns) | |
->first(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment