Created
August 21, 2017 21:44
-
-
Save edilsoncichon/0047d3f631e469e73bfa292a70f1a8a6 to your computer and use it in GitHub Desktop.
Utilities for model Eloquent Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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