Created
December 18, 2019 02:10
-
-
Save MartinTale/afaa9c5f1fec2cb0c0ceb54bedef5df4 to your computer and use it in GitHub Desktop.
Laravel trait to support composite primary keys in Eloquent models..
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\Traits; | |
use Exception; | |
use Illuminate\Database\Eloquent\Builder; | |
trait HasCompositePrimaryKeyTrait | |
{ | |
/** | |
* Get the value indicating whether the IDs are incrementing. | |
* | |
* @return bool | |
*/ | |
public function getIncrementing() | |
{ | |
return false; | |
} | |
/** | |
* Get the value of the model's primary key. | |
* | |
* @return mixed | |
*/ | |
public function getKey() | |
{ | |
$attributes = []; | |
foreach ($this->getKeyName() as $key) { | |
$attributes[$key] = $this->getAttribute($key); | |
} | |
return $attributes; | |
} | |
/** | |
* Set the keys for a save update query. | |
* | |
* @param \Illuminate\Database\Eloquent\Builder $query | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
protected function setKeysForSaveQuery(Builder $query) | |
{ | |
foreach ($this->getKeyName() as $key) { | |
if (isset($this->$key)) | |
$query->where($key, '=', $this->$key); | |
else | |
throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key); | |
} | |
return $query; | |
} | |
/** | |
* Execute a query for a single record by ID. | |
* | |
* @param array $ids Array of keys, like [column => value]. | |
* @param array $columns | |
* @return mixed|static | |
*/ | |
public static function find($ids, $columns = ['*']) | |
{ | |
$me = new self; | |
$query = $me->newQuery(); | |
foreach ($me->getKeyName() as $key) { | |
$query->where($key, '=', $ids[$key]); | |
} | |
return $query->first($columns); | |
} | |
/** | |
* Find a model by its primary key or throw an exception. | |
* | |
* @param mixed $ids | |
* @param array $columns | |
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection | |
* | |
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException | |
*/ | |
public static function findOrFail($ids, $columns = ['*']) | |
{ | |
$result = self::find($ids, $columns); | |
if (!is_null($result)) { | |
return $result; | |
} | |
throw (new ModelNotFoundException)->setModel( | |
__CLASS__, $ids | |
); | |
} | |
/** | |
* Reload the current model instance with fresh attributes from the database. | |
* | |
* @return $this | |
*/ | |
public function refresh() | |
{ | |
if (!$this->exists) { | |
return $this; | |
} | |
$this->setRawAttributes( | |
static::findOrFail($this->getKey())->attributes | |
); | |
$this->load(collect($this->relations)->except('pivot')->keys()->toArray()); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment