File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 22
33namespace Helldar \LaravelSupport \Eloquent ;
44
5+ use Illuminate \Contracts \Support \Arrayable ;
56use Illuminate \Database \Eloquent \Model ;
67
78abstract 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}
You can’t perform that action at this time.
0 commit comments