Skip to content

Commit 677b5ae

Browse files
committed
Added deafult sort
1 parent b2246bd commit 677b5ae

File tree

5 files changed

+129
-55
lines changed

5 files changed

+129
-55
lines changed

config/crud.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
'search' => env('CRUD_DISPLAY_SEARCH', false),
66
'filters' => env('CRUD_DISPLAY_FILTERS', true),
77
],
8-
];
8+
];

resources/views/includes/sort-link.blade.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
@php
22
$down_fill = 'lightgray';
33
$up_fill = 'lightgray';
4-
$attribute = $attribute ?? '';
4+
$attribute = $field['attribute'] ?? '';
55
$identifier = $identifier ?? '';
6-
$label = $label ?? '';
7-
if(request()->query($identifier.'sort') == $attribute) {
6+
$defaultSort = $field['defaultSort'] ?? null;
7+
$label = $field['label'] ?? $field['attribute'] ?? '';
8+
if(request()->query($identifier.'sort') == $attribute || (!request()->query($identifier.'sort') && $defaultSort == 'asc')) {
89
$up_fill = 'black';
910
}
10-
if(request()->query($identifier.'sort') == '-'.$attribute) {
11+
if(request()->query($identifier.'sort') == '-'.$attribute || (!request()->query($identifier.'sort') && $defaultSort == 'desc')) {
1112
$down_fill = 'black';
1213
}
1314
@endphp

resources/views/list.blade.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
@can('adminCreate', new $model)
2-
<div class="flex flex-row-reverse d-print-none with-border">
3-
<a href="{{ $routes['create'] }}" class="btn btn-primary">{{ __('Add') }}</a>
4-
</div>
5-
@endcan
1+
@isset($routes['create'])
2+
@can('adminCreate', new $model)
3+
<div class="flex flex-row-reverse d-print-none with-border">
4+
<a href="{{ $routes['create'] }}" class="btn btn-primary">{{ __('Add') }}</a>
5+
</div>
6+
@endcan
7+
@endisset
68

79
<div class="py-2">
810
<div class="min-w-full border-base-200 shadow overflow-x-auto">
@@ -15,7 +17,7 @@
1517
@if ($field['list'] ?? true)
1618
<th class="py-2 px-4 bg-base-50 font-bold uppercase text-sm text-left">
1719
@if ($field['sortable'])
18-
@include('crud::includes.sort-link', ['label' => $field['label'] ?? $field['attribute'], 'attribute' => $field['attribute']])
20+
@include('crud::includes.sort-link', ['field' => $field])
1921
@else
2022
{{ $field['label'] ?? $field['attribute'] }}
2123
@endif
@@ -43,14 +45,17 @@
4345
<td class="p-4">
4446
<form action="{{ $routes['destroy']($item->id) }}" method="POST">
4547
<div>
48+
@isset ($routes['edit'])
4649
@can('adminUpdate', $item)
4750
<a href="{{$routes['edit']($item->id)}}" class="btn btn-square btn-ghost">
4851
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
4952
<path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10" />
5053
</svg>
5154
</a>
5255
@endcan
56+
@endisset
5357

58+
@isset ($routes['destroy'])
5459
@can('adminDelete', $item)
5560
@csrf
5661
@method('DELETE')
@@ -63,6 +68,7 @@
6368
</svg>
6469
</button>
6570
@endcan
71+
@endisset
6672
</div>
6773
</form>
6874
</td>

src/CrudBuilder.php

+109-43
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use BalajiDharma\LaravelFormBuilder\Facades\FormBuilder;
66
use Exception;
77
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Config;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Schema;
11-
use Illuminate\Http\Request;
1212

1313
class CrudBuilder
1414
{
@@ -76,12 +76,14 @@ public function setIdentifier()
7676
public function setAddtional($addtional)
7777
{
7878
$this->addtional = array_merge($this->addtional, $addtional);
79+
7980
return $this;
8081
}
8182

8283
public function setTitle($title)
8384
{
8485
$this->title = $title;
86+
8587
return $this;
8688
}
8789

@@ -92,18 +94,21 @@ public function setRedirectUrl($redirectUrl = null)
9294
} else {
9395
$this->redirectUrl = url()->current();
9496
}
97+
9598
return $this;
9699
}
97100

98101
public function setDisplayFilters(bool $displayFilters = true)
99102
{
100103
$this->displayFilters = $displayFilters;
104+
101105
return $this;
102106
}
103107

104108
public function setDisplaySearch(bool $displaySearch = true)
105109
{
106110
$this->displaySearch = $displaySearch;
111+
107112
return $this;
108113
}
109114

@@ -185,16 +190,15 @@ private function buildRows($columns = [])
185190
$tableName = $model->getTable();
186191

187192
// Get all column names and their types for the table
188-
$tableColumns = Schema::getColumnListing($tableName);
189-
193+
$tableColumns = Schema::getColumns($tableName);
190194
foreach ($columns as $column) {
191195
$attribute = $column['attribute'] ?? null;
192196
$type = $column['type'] ?? 'custom';
193197
$fillable = false;
194198
$primaryKey = false;
195-
196-
if ($attribute && ! isset($column['type']) && in_array($attribute, $tableColumns)) {
197-
$type = DB::getSchemaBuilder()->getColumnType($tableName, $attribute);
199+
$tableColumn = collect($tableColumns)->where('name', $attribute)->first();
200+
if ($attribute && ! isset($column['type']) && $tableColumn) {
201+
$type = $tableColumn['type_name'];
198202
$type = $this->crudHelper->getInputType($type);
199203
$primaryKey = $attribute == $model->getKeyName();
200204
}
@@ -253,7 +257,7 @@ public function applyFilters()
253257
}
254258
}
255259
}
256-
if (!$hasFilters) {
260+
if (! $hasFilters) {
257261
$this->displayFilters = false;
258262
}
259263
}
@@ -306,19 +310,29 @@ public function applySorting()
306310
}
307311
$field = collect($this->fields)->where('attribute', $attribute)->first() ?? null;
308312

309-
if ($field && isset($field['sortable'])) {
310-
if (isset($field['relation'])) {
311-
$relation = $field['relation'];
312-
$relationField = $field['relation_field'] ?? $field['attribute'];
313-
$this->dataProvider->whereHas($relation, function ($q) use ($relationField, $sortOrder) {
314-
$table = $q->getModel()->getTable();
315-
$qualifiedField = "{$table}.{$relationField}";
316-
$q->orderBy($qualifiedField, $sortOrder);
317-
});
318-
} else {
319-
$table = $this->dataProvider->getModel()->getTable();
320-
$this->dataProvider->orderBy("{$table}.{$attribute}", $sortOrder);
321-
}
313+
$this->applyFieldSort($field, $sortOrder, $attribute);
314+
} else {
315+
$field = collect($this->fields)->whereNotNull('defaultSort')->first() ?? null;
316+
if ($field) {
317+
$this->applyFieldSort($field, $field['defaultSort'], $field['attribute']);
318+
}
319+
}
320+
}
321+
322+
public function applyFieldSort($field, $sortOrder, $attribute)
323+
{
324+
if ($field && isset($field['sortable'])) {
325+
if (isset($field['relation'])) {
326+
$relation = $field['relation'];
327+
$relationField = $field['relation_field'] ?? $field['attribute'];
328+
$this->dataProvider->whereHas($relation, function ($q) use ($relationField, $sortOrder) {
329+
$table = $q->getModel()->getTable();
330+
$qualifiedField = "{$table}.{$relationField}";
331+
$q->orderBy($qualifiedField, $sortOrder);
332+
});
333+
} else {
334+
$table = $this->dataProvider->getModel()->getTable();
335+
$this->dataProvider->orderBy("{$table}.{$attribute}", $sortOrder);
322336
}
323337
}
324338
}
@@ -362,23 +376,42 @@ public function buildRoutes($mainRoute = null)
362376
$mainRoute = substr($routeName, 0, strrpos($routeName, '.'));
363377
}
364378

365-
return [
366-
'index' => $this->route($mainRoute.'.index'),
367-
'create' => $this->route($mainRoute.'.create'),
368-
'store' => $this->route($mainRoute.'.store'),
369-
'edit' => function ($id) use ($mainRoute) {
370-
return $this->route($mainRoute.'.edit', $id);
371-
},
372-
'update' => function ($id) use ($mainRoute) {
373-
return $this->route($mainRoute.'.update', $id);
374-
},
375-
'show' => function ($id) use ($mainRoute) {
376-
return $this->route($mainRoute.'.show', $id);
377-
},
378-
'destroy' => function ($id) use ($mainRoute) {
379-
return $this->route($mainRoute.'.destroy', $id);
380-
},
381-
];
379+
$routes = [];
380+
foreach ($this->getActions() as $action => $value) {
381+
if (!$value) {
382+
continue;
383+
}
384+
switch ($action) {
385+
case 'create':
386+
$routes['create'] = $this->route($mainRoute.'.create');
387+
break;
388+
case 'store':
389+
$routes['store'] = $this->route($mainRoute.'.store');
390+
break;
391+
case 'edit':
392+
$routes['edit'] = function ($id) use ($mainRoute) {
393+
return $this->route($mainRoute.'.edit', $id);
394+
};
395+
break;
396+
case 'update':
397+
$routes['update'] = function ($id) use ($mainRoute) {
398+
return $this->route($mainRoute.'.update', $id);
399+
};
400+
break;
401+
case 'show':
402+
$routes['show'] = function ($id) use ($mainRoute) {
403+
return $this->route($mainRoute.'.show', $id);
404+
};
405+
break;
406+
case 'destroy':
407+
$routes['destroy'] = function ($id) use ($mainRoute) {
408+
return $this->route($mainRoute.'.destroy', $id);
409+
};
410+
break;
411+
}
412+
}
413+
414+
return $routes;
382415
}
383416

384417
public function render($view)
@@ -412,7 +445,7 @@ protected function commonRenderData()
412445
'identifier' => $this->identifier,
413446
'redirectUrl' => $this->redirectUrl,
414447
'displaySearch' => $this->displaySearch,
415-
'displayFilters' => $this->displayFilters
448+
'displayFilters' => $this->displayFilters,
416449
];
417450
}
418451

@@ -454,7 +487,7 @@ public function buildForm()
454487
}
455488
}
456489

457-
if($this->request->input('_redirect')){
490+
if ($this->request->input('_redirect')) {
458491
$form->add('_redirect', 'hidden', [
459492
'value' => $this->request->input('_redirect'),
460493
]);
@@ -469,8 +502,7 @@ public function buildForm()
469502

470503
public function route($name, $parameters = [], $absolute = true)
471504
{
472-
if ($this->redirectUrl)
473-
{
505+
if ($this->redirectUrl) {
474506
return $this->mergeQueryParams(route($name, $parameters), ['_redirect' => $this->redirectUrl]);
475507
} else {
476508
return route($name, $parameters);
@@ -495,18 +527,52 @@ public function mergeQueryParams($url, array $parameters = [])
495527

496528
// Remove null/empty parameters
497529
$query = array_filter($query, function ($value) {
498-
return !is_null($value) && $value !== '';
530+
return ! is_null($value) && $value !== '';
499531
});
500532

501533
// Build base URL without query string
502534
$baseUrl = explode('?', $url)[0];
503535

504536
// Return URL with merged query parameters
505537
return $query
506-
? $baseUrl . '?' . http_build_query($query)
538+
? $baseUrl.'?'.http_build_query($query)
507539
: $baseUrl;
508540
} catch (\Exception $e) {
509541
return $url;
510542
}
511543
}
544+
545+
public function getActions()
546+
{
547+
$default = [
548+
'index' => true,
549+
'create' => true,
550+
'store' => true,
551+
'edit' => true,
552+
'update' => true,
553+
'show' => true,
554+
'destroy' => true,
555+
];
556+
return array_merge($default, $this->actions ?? []);
557+
}
558+
559+
560+
public function setActions(array $actions)
561+
{
562+
$this->actions = $actions;
563+
}
564+
565+
566+
public function getAction($action)
567+
{
568+
$actions = $this->getActions();
569+
return $actions[$action] ?? false;
570+
}
571+
572+
public function setAction($action, $value)
573+
{
574+
$actions = $this->getActions();
575+
$actions[$action] = $value;
576+
$this->setActions($actions);
577+
}
512578
}

src/helpers.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function crud(CrudBuilder $crud, $view = 'list')
1616
function crudRedirect($name, $message)
1717
{
1818
$redirectUrl = request()->input('_redirect', route($name));
19+
1920
return redirect($redirectUrl)->withMessage(__($message));
2021
}
2122

22-
}
23+
}

0 commit comments

Comments
 (0)