Skip to content

Instantly share code, notes, and snippets.

@edilsoncichon
Created August 21, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edilsoncichon/0047d3f631e469e73bfa292a70f1a8a6 to your computer and use it in GitHub Desktop.
Save edilsoncichon/0047d3f631e469e73bfa292a70f1a8a6 to your computer and use it in GitHub Desktop.
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