Skip to content

Commit ee2e5c0

Browse files
author
Andrey Helldar
committed
Added an override for the find method
1 parent bddae0d commit ee2e5c0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: src/Eloquent/CompositeKeysModel.php

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Helldar\LaravelSupport\Eloquent;
44

5+
use Illuminate\Contracts\Support\Arrayable;
56
use Illuminate\Database\Eloquent\Model;
67

78
abstract class CompositeKeysModel extends Model
@@ -24,6 +25,23 @@ public function getAttribute($key)
2425
: null;
2526
}
2627

28+
public function find($id, $columns = ['*'])
29+
{
30+
if (is_array($id)) {
31+
$keys = array_filter($id, function ($key) {
32+
return $this->hasPrimary($key);
33+
}, ARRAY_FILTER_USE_KEY);
34+
35+
if (! empty($keys)) {
36+
return $this->where($id)->first($columns);
37+
}
38+
}
39+
40+
return $id instanceof Arrayable
41+
? $this->findMany($id, $columns)
42+
: $this->whereKey($id)->first($columns);
43+
}
44+
2745
/**
2846
* @param \Illuminate\Database\Eloquent\Builder $query
2947
*
@@ -44,4 +62,9 @@ protected function setKeysForSaveQuery($query)
4462

4563
return $query;
4664
}
65+
66+
protected function hasPrimary(string $key): bool
67+
{
68+
return in_array($key, $this->primaryKey);
69+
}
4770
}

0 commit comments

Comments
 (0)