Skip to content

Instantly share code, notes, and snippets.

@codyphobe
Forked from mpociot/SingletonModel.php
Created February 15, 2020 04:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codyphobe/66928ab81a7110e8bb4c7585f98cc81a to your computer and use it in GitHub Desktop.
Making it easier to work with single row models in Laravel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SingletonModel extends Model
{
protected function store($data = [])
{
return $this->updateOrCreate($this->singletonIdentifiableBy(), $data);
}
public function singletonIdentifiableBy()
{
return [
$this->getKeyName() => optional($this->first())->getKey()
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment