Created
September 1, 2020 14:19
-
-
Save mathieutu/0492a50200a52958138d3bbcb668236f to your computer and use it in GitHub Desktop.
Laravel base Model
This file contains hidden or 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\Models; | |
use Illuminate\Support\Str; | |
use MathieuTu\Exporter\Exporter; | |
abstract class Model extends \Illuminate\Database\Eloquent\Model | |
{ | |
use HasUuid, Exporter; | |
protected $guarded = []; | |
/** | |
* @return static | |
*/ | |
public static function fake($attributes = []): self | |
{ | |
return static::fCreate($attributes); | |
} | |
/** | |
* @return static | |
*/ | |
public static function fCreate($attributes = []): self | |
{ | |
return static::factory()->create($attributes); | |
} | |
public static function factory() | |
{ | |
return factory(static::class, ...func_get_args()); | |
} | |
public static function fRaw($attributes = []): array | |
{ | |
return static::factory()->raw($attributes); | |
} | |
/** | |
* @return static | |
*/ | |
public static function fMake($attributes = []): self | |
{ | |
return static::factory()->make($attributes); | |
} | |
/** | |
* @return static|\Closure | |
*/ | |
public static function randomOrFactory(array $attributes = [], string $method = 'lazy') | |
{ | |
return static::query() | |
->where($attributes) | |
->inRandomOrder() | |
->firstOr(fn() => static::factory()->$method($attributes)); | |
} | |
/** | |
* @return static | |
*/ | |
public static function randomOrFake(array $attributes = []) | |
{ | |
return static::randomOrFactory($attributes, 'create'); | |
} | |
/** | |
* @return static | |
*/ | |
public static function firstOrFake(array $attributes = []) | |
{ | |
return static::firstOrFactory($attributes, 'create'); | |
} | |
/** | |
* @return static|\Closure | |
*/ | |
public static function firstOrFactory(array $attributes = [], string $method = 'lazy') | |
{ | |
return static::query() | |
->where($attributes) | |
->oldest() | |
->firstOr(fn() => static::factory()->$method($attributes)); | |
} | |
/** | |
* @return static | |
*/ | |
public static function new(array $attributes = []) | |
{ | |
return new static($attributes); | |
} | |
/** | |
* Allow exporting attributes directly in camelCase to frontend. | |
* | |
* @param string $key | |
* @return mixed | |
*/ | |
public function getAttribute($key) | |
{ | |
if (array_key_exists($key, $this->getAttributes()) | |
|| array_key_exists($key, $this->relations) | |
|| method_exists($this, $key) | |
) { | |
return parent::getAttribute($key); | |
} | |
return parent::getAttribute(Str::snake($key)); | |
} | |
/** | |
* Allow setting attributes directly in camelCase from frontend. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @return Model|mixed | |
*/ | |
public function setAttribute($key, $value) | |
{ | |
return parent::setAttribute(Str::snake($key), $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment