public function get($primaryKey, $options = [])
{
$key = (array) $this->primaryKey();
$alias = $this->alias();
foreach ($key as $index => $keyname) {
$key[$index] = $alias . '.' . $keyname;
}
$primaryKey = (array) $primaryKey;
if (count($key) !== count($primaryKey)) {
$primaryKey = $primaryKey ?: [null];
$primaryKey = array_map(function ($key) {
return var_export($key, true);
}, $primaryKey);
throw new InvalidPrimaryKeyException(sprintf('Record not found in table "%s" with primary key [%s]', $this->table(), implode($primaryKey, ', ')));
}
$conditions = array_combine($key, $primaryKey);
$cacheConfig = isset($options['cache']) ? $options['cache'] : false;
$cacheKey = isset($options['key']) ? $options['key'] : false;
$finder = isset($options['finder']) ? $options['finder'] : 'all';
unset($options['key'], $options['cache'], $options['finder']);
$query = $this->find($finder, $options)->where($conditions);
if ($cacheConfig) {
if (!$cacheKey) {
$cacheKey = sprintf("get:%s.%s%s", $this->connection()->configName(), $this->table(), json_encode($primaryKey));
}
$query->cache($cacheKey, $cacheConfig);
}
return $query->firstOrFail();
}