More components for forms, updated base component, updated CompsiteKeys based on thiagoprz/eloquent-composite-key

This commit is contained in:
Deon George 2024-07-23 18:47:36 +10:00
parent b9a3cd5647
commit f32c29fa8c
12 changed files with 133 additions and 75 deletions

View File

@ -0,0 +1,11 @@
<button type="cancel" id="cancel" {{ $attributes->class(['btn','btn-sm','btn-danger']) }}>Cancel</button>
@section('page-scripts')
<script type="text/javascript">
$(document).ready(function() {
$('#cancel').on('click',function(item) {
return history.back();
});
});
</script>
@append

View File

@ -0,0 +1 @@
<button type="reset" id="reset" {{ $attributes->class(['btn','btn-sm','btn-secondary']) }}>Reset</button>

View File

@ -0,0 +1 @@
<button type="submit" id="submit" {{ $attributes->class(['btn','btn-sm','btn-success']) }}>{{ $slot }}</button>

View File

@ -0,0 +1 @@
@session('success')<button {{ $attributes->class(['btn','btn-sm','btn-success']) }}>{{ $value }}</button>@endsession

View File

@ -2,17 +2,24 @@
@if(isset($label))
<label for="{{ $id ?? $name }}">{{ $label }}</label>
@endisset
<div class="input-group has-validation">
@isset($icon)
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-fw {{ $icon }}"></i></span>
</div>
@endisset()
{{ $slot }}
<span class="invalid-feedback" role="alert">
@error($name)
{{ $message }}
@enderror
</span>
<div class="form-group">
<div class="input-group has-validation">
@if(isset($icon) || isset($prepend))
<div class="input-group-prepend">
<span class="input-group-text">@isset($icon)<i class="fas fa-fw {{ $icon }}"></i>@endisset @isset($prepend){!! $prepend !!}@endisset</span>
</div>
@endif()
{{ $slot }}
<span class="invalid-feedback" role="alert">
@error($name)
{{ $message }}
@elseif(isset($feedback))
{{ $feedback }}
@enderror
</span>
</div>
@isset($helper)
<span class="input-helper">{{ $helper }}</span>
@endif
</div>
</div>

View File

@ -0,0 +1,17 @@
<x-leenooks::form.base {{ $attributes }}>
<input type="file" class="form-control {{ $classes ?? ''}}@error($name) is-invalid @enderror" id="{{ $id ?? $name }}" name="{{ $name }}" @readonly(isset($readonly)) @required(isset($required))>
</x-leenooks::form.base>
{{ $slot }}
@section('page-scripts')
<script>
$(document).ready(function() {
$('#{{ $id ?? $name }}').on('change',function(){
//get the file name
var fileName = $(this).val();
//replace the "Choose a file" label
$(this).parent().next('.input-helper').html(fileName);
})
});
</script>
@append

View File

@ -7,14 +7,14 @@
@empty($groupby)
@foreach($options as $option)
@continue(! Arr::get($option,'value'))
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == old($name,$value))>{{ Arr::get($option,'value') }}</option>
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == old($name,$value ?? ''))>{{ Arr::get($option,'value') }}</option>
@endforeach
@else
@foreach($options->groupBy($groupby) as $group)
<optgroup label="{{ $groupby == 'active' ? (Arr::get($group->first(),$groupby) ? 'Active' : 'Not Active') : Arr::get($group->first(),$groupby) }}">
@foreach($group as $option)
@empty(Arr::get($option,'value')) @continue @endempty
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == old($old,$value))>{{ Arr::get($option,'value') }}</option>
@continue(! Arr::get($option,'value'))
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == old($old,$value ?? ''))>{{ Arr::get($option,'value') }}</option>
@endforeach
</optgroup>
@endforeach

View File

@ -1,3 +1,3 @@
<x-leenooks::form.base {{ $attributes }}>
<input type="text" class="form-control {{ $classes ?? ''}}@error($name) is-invalid @enderror" id="{{ $id ?? $name }}" name="{{ $name }}" value="{{ old($name,$value ?? "") }}">
<input type="text" class="form-control {{ $classes ?? ''}}@error($name) is-invalid @enderror" id="{{ $id ?? $name }}" name="{{ $name }}" value="{{ old($name,$value ?? "") }}" @readonly(isset($readonly)) @required(isset($required))>
</x-leenooks::form.base>

View File

@ -0,0 +1,3 @@
<x-leenooks::form.base {{ $attributes }}>
<textarea class="form-control {{ $classes ?? ''}} @error($name) is-invalid @enderror" id="{{ $id ?? $name }}" name="{{ $name }}" @isset($placeholder)placeholder="{{ $placeholder }}"@endisset>{{ old($name,$value ?? "") }}</textarea>
</x-leenooks::form.base>

View File

@ -1 +0,0 @@
<button {{ $attributes->class(['btn','btn-sm','btn-success']) }}>{{ $slot }}</button>

View File

@ -1,39 +0,0 @@
<div class="form-group">
<label for="{{ $id }}">{{ $label }}</label>
<div class="input-group has-validation">
@empty($icon)
@else
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa-fw {{ $icon }}"></i></span>
</div>
@endempty()
<div class="custom-file">
<input type="file" class="custom-file-input @error($id) is-invalid @enderror" id="{{ $id }}" name="{{ $name ?? $id}}">
<label class="custom-file-label input-group-text" for="{{ $id }}">Choose file</label>
</div>
{{--
<div class="input-group-append">
<button type="submit" class="input-group-text btn btn-success" id="">Upload</button>
</div>
--}}
<span class="invalid-feedback @error($id) d-block @enderror" role="alert">
@error($id)
{{ $message }}
@enderror
</span>
</div>
</div>
@section('page-scripts')
<script>
$(document).ready(function() {
$('#{{ $id }}').on('change',function(){
//get the file name
var fileName = $(this).val();
//replace the "Choose a file" label
$(this).next('.custom-file-label').html(fileName);
})
});
</script>
@append

View File

@ -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;
}
}