File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Helldar \LaravelSupport \Eloquent ;
4
4
5
+ use Illuminate \Contracts \Support \Arrayable ;
5
6
use Illuminate \Database \Eloquent \Model ;
6
7
7
8
abstract class CompositeKeysModel extends Model
@@ -24,6 +25,23 @@ public function getAttribute($key)
24
25
: null ;
25
26
}
26
27
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
+
27
45
/**
28
46
* @param \Illuminate\Database\Eloquent\Builder $query
29
47
*
@@ -44,4 +62,9 @@ protected function setKeysForSaveQuery($query)
44
62
45
63
return $ query ;
46
64
}
65
+
66
+ protected function hasPrimary (string $ key ): bool
67
+ {
68
+ return in_array ($ key , $ this ->primaryKey );
69
+ }
47
70
}
You can’t perform that action at this time.
0 commit comments