-
-
Save codyphobe/66928ab81a7110e8bb4c7585f98cc81a to your computer and use it in GitHub Desktop.
Making it easier to work with single row models in 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; | |
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