Skip to content

Instantly share code, notes, and snippets.

@mpociot
Created January 24, 2020 13:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mpociot/43720e372505d76ec9c49240f63c4a4b to your computer and use it in GitHub Desktop.
Save mpociot/43720e372505d76ec9c49240f63c4a4b 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