From f32c29fa8c4b189add48bde26b7b7115be49355f Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 23 Jul 2024 18:47:36 +1000 Subject: [PATCH] More components for forms, updated base component, updated CompsiteKeys based on thiagoprz/eloquent-composite-key --- resources/components/button/cancel.blade.php | 11 +++ resources/components/button/reset.blade.php | 1 + resources/components/button/submit.blade.php | 1 + resources/components/button/success.blade.php | 1 + resources/components/form/base.blade.php | 31 +++--- resources/components/form/file.blade.php | 17 ++++ resources/components/form/select.blade.php | 6 +- resources/components/form/text.blade.php | 2 +- resources/components/form/textarea.blade.php | 3 + resources/components/success.blade.php | 1 - .../adminlte/views/widget/form_file.blade.php | 39 -------- src/Traits/CompositeKeys.php | 95 +++++++++++++++---- 12 files changed, 133 insertions(+), 75 deletions(-) create mode 100644 resources/components/button/cancel.blade.php create mode 100644 resources/components/button/reset.blade.php create mode 100644 resources/components/button/submit.blade.php create mode 100644 resources/components/button/success.blade.php create mode 100644 resources/components/form/file.blade.php create mode 100644 resources/components/form/textarea.blade.php delete mode 100644 resources/components/success.blade.php delete mode 100644 resources/themes/adminlte/views/widget/form_file.blade.php diff --git a/resources/components/button/cancel.blade.php b/resources/components/button/cancel.blade.php new file mode 100644 index 0000000..65634c1 --- /dev/null +++ b/resources/components/button/cancel.blade.php @@ -0,0 +1,11 @@ + + +@section('page-scripts') + +@append \ No newline at end of file diff --git a/resources/components/button/reset.blade.php b/resources/components/button/reset.blade.php new file mode 100644 index 0000000..39b3119 --- /dev/null +++ b/resources/components/button/reset.blade.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/components/button/submit.blade.php b/resources/components/button/submit.blade.php new file mode 100644 index 0000000..821ef27 --- /dev/null +++ b/resources/components/button/submit.blade.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/components/button/success.blade.php b/resources/components/button/success.blade.php new file mode 100644 index 0000000..113de13 --- /dev/null +++ b/resources/components/button/success.blade.php @@ -0,0 +1 @@ +@session('success')@endsession \ No newline at end of file diff --git a/resources/components/form/base.blade.php b/resources/components/form/base.blade.php index 271be94..d5ee26a 100644 --- a/resources/components/form/base.blade.php +++ b/resources/components/form/base.blade.php @@ -2,17 +2,24 @@ @if(isset($label)) @endisset -
- @isset($icon) -
- -
- @endisset() - {{ $slot }} - - @error($name) - {{ $message }} - @enderror - +
+
+ @if(isset($icon) || isset($prepend)) +
+ @isset($icon)@endisset @isset($prepend){!! $prepend !!}@endisset +
+ @endif() + {{ $slot }} + + @error($name) + {{ $message }} + @elseif(isset($feedback)) + {{ $feedback }} + @enderror + +
+ @isset($helper) + {{ $helper }} + @endif
\ No newline at end of file diff --git a/resources/components/form/file.blade.php b/resources/components/form/file.blade.php new file mode 100644 index 0000000..764dbb5 --- /dev/null +++ b/resources/components/form/file.blade.php @@ -0,0 +1,17 @@ + + + +{{ $slot }} + +@section('page-scripts') + +@append \ No newline at end of file diff --git a/resources/components/form/select.blade.php b/resources/components/form/select.blade.php index 5d4a79b..17a32d2 100644 --- a/resources/components/form/select.blade.php +++ b/resources/components/form/select.blade.php @@ -7,14 +7,14 @@ @empty($groupby) @foreach($options as $option) @continue(! Arr::get($option,'value')) - + @endforeach @else @foreach($options->groupBy($groupby) as $group) @foreach($group as $option) - @empty(Arr::get($option,'value')) @continue @endempty - + @continue(! Arr::get($option,'value')) + @endforeach @endforeach diff --git a/resources/components/form/text.blade.php b/resources/components/form/text.blade.php index 7858650..f7e511d 100644 --- a/resources/components/form/text.blade.php +++ b/resources/components/form/text.blade.php @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/resources/components/form/textarea.blade.php b/resources/components/form/textarea.blade.php new file mode 100644 index 0000000..0b371b2 --- /dev/null +++ b/resources/components/form/textarea.blade.php @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/resources/components/success.blade.php b/resources/components/success.blade.php deleted file mode 100644 index 6edb2db..0000000 --- a/resources/components/success.blade.php +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/resources/themes/adminlte/views/widget/form_file.blade.php b/resources/themes/adminlte/views/widget/form_file.blade.php deleted file mode 100644 index 8c787d7..0000000 --- a/resources/themes/adminlte/views/widget/form_file.blade.php +++ /dev/null @@ -1,39 +0,0 @@ -
- -
- @empty($icon) - @else -
- -
- @endempty() -
- - -
- - {{-- -
- -
- --}} - - @error($id) - {{ $message }} - @enderror - -
-
- -@section('page-scripts') - -@append \ No newline at end of file diff --git a/src/Traits/CompositeKeys.php b/src/Traits/CompositeKeys.php index 3564745..df9d3bf 100644 --- a/src/Traits/CompositeKeys.php +++ b/src/Traits/CompositeKeys.php @@ -25,33 +25,90 @@ trait CompositeKeys { { $keys = $this->getKeyName(); - if (! is_array($keys)) { - return parent::setKeysForSaveQuery($query); - } - - foreach($keys as $keyName) { - $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName)); - } - - return $query; + return !is_array($keys) + ? parent::setKeysForSaveQuery($query) + : $query->where(function($q) use($keys) { + foreach ($keys as $key){ + $q->where($key,$this->getAttribute($key)); + } + }); } /** - * Get the primary key value for a save query. + * Get the casts array. + * + * @return array + */ + public function getCasts() + { + if ($this->getIncrementing()) + return array_merge([$this->getKeyName() => $this->getKeyType()],$this->casts); + + return $this->casts; + } + + /** + * @return false + */ + public function getIncrementing() + { + return FALSE; + } + + /** + * Get the value of the model's primary key. * - * @param mixed $keyName * @return mixed */ - protected function getKeyForSaveQuery($keyName = null) + public function getKey() { - if (is_null($keyName)) { - $keyName = $this->getKeyName(); - } + $fields = $this->getKeyName(); + $keys = []; - if (isset($this->original[$keyName])) { - return $this->original[$keyName]; - } + array_map(function($key) use(&$keys) { + $keys[] = $this->getAttribute($key); + }, $fields); - return $this->getAttribute($keyName); + return $keys; + } + + /** + * Finds model by primary keys + * + * @param array $ids + * @return mixed + */ + public static function find(array $ids) + { + $modelClass = get_called_class(); + $model = new $modelClass(); + $keys = $model->primaryKey; + + return $model->where(function($query) use($ids,$keys) { + foreach ($keys as $idx => $key) { + if (isset($ids[$idx])) + $query->where($key, $ids[$idx]); + else + $query->whereNull($key); + } + })->first(); + } + + /** + * Find model by primary key or throws ModelNotFoundException + * + * @param array $ids + * @return mixed + */ + public static function findOrFail(array $ids) + { + $modelClass = get_called_class(); + $model = new $modelClass(); + $record = $model->find($ids); + + if (! $record) + throw new ModelNotFoundException; + + return $record; } } \ No newline at end of file