Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: balajidharma/laravel-crud
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.7
Choose a base ref
...
head repository: balajidharma/laravel-crud
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.0.8
Choose a head ref
  • 1 commit
  • 5 files changed
  • 1 contributor

Commits on Dec 9, 2024

  1. Added deafult sort

    balajidharma committed Dec 9, 2024
    Copy the full SHA
    677b5ae View commit details
Showing with 129 additions and 55 deletions.
  1. +1 −1 config/crud.php
  2. +5 −4 resources/views/includes/sort-link.blade.php
  3. +12 −6 resources/views/list.blade.php
  4. +109 −43 src/CrudBuilder.php
  5. +2 −1 src/helpers.php
2 changes: 1 addition & 1 deletion config/crud.php
Original file line number Diff line number Diff line change
@@ -5,4 +5,4 @@
'search' => env('CRUD_DISPLAY_SEARCH', false),
'filters' => env('CRUD_DISPLAY_FILTERS', true),
],
];
];
9 changes: 5 additions & 4 deletions resources/views/includes/sort-link.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
@php
$down_fill = 'lightgray';
$up_fill = 'lightgray';
$attribute = $attribute ?? '';
$attribute = $field['attribute'] ?? '';
$identifier = $identifier ?? '';
$label = $label ?? '';
if(request()->query($identifier.'sort') == $attribute) {
$defaultSort = $field['defaultSort'] ?? null;
$label = $field['label'] ?? $field['attribute'] ?? '';
if(request()->query($identifier.'sort') == $attribute || (!request()->query($identifier.'sort') && $defaultSort == 'asc')) {
$up_fill = 'black';
}
if(request()->query($identifier.'sort') == '-'.$attribute) {
if(request()->query($identifier.'sort') == '-'.$attribute || (!request()->query($identifier.'sort') && $defaultSort == 'desc')) {
$down_fill = 'black';
}
@endphp
18 changes: 12 additions & 6 deletions resources/views/list.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@can('adminCreate', new $model)
<div class="flex flex-row-reverse d-print-none with-border">
<a href="{{ $routes['create'] }}" class="btn btn-primary">{{ __('Add') }}</a>
</div>
@endcan
@isset($routes['create'])
@can('adminCreate', new $model)
<div class="flex flex-row-reverse d-print-none with-border">
<a href="{{ $routes['create'] }}" class="btn btn-primary">{{ __('Add') }}</a>
</div>
@endcan
@endisset

<div class="py-2">
<div class="min-w-full border-base-200 shadow overflow-x-auto">
@@ -15,7 +17,7 @@
@if ($field['list'] ?? true)
<th class="py-2 px-4 bg-base-50 font-bold uppercase text-sm text-left">
@if ($field['sortable'])
@include('crud::includes.sort-link', ['label' => $field['label'] ?? $field['attribute'], 'attribute' => $field['attribute']])
@include('crud::includes.sort-link', ['field' => $field])
@else
{{ $field['label'] ?? $field['attribute'] }}
@endif
@@ -43,14 +45,17 @@
<td class="p-4">
<form action="{{ $routes['destroy']($item->id) }}" method="POST">
<div>
@isset ($routes['edit'])
@can('adminUpdate', $item)
<a href="{{$routes['edit']($item->id)}}" class="btn btn-square btn-ghost">
<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">
<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" />
</svg>
</a>
@endcan
@endisset

@isset ($routes['destroy'])
@can('adminDelete', $item)
@csrf
@method('DELETE')
@@ -63,6 +68,7 @@
</svg>
</button>
@endcan
@endisset
</div>
</form>
</td>
152 changes: 109 additions & 43 deletions src/CrudBuilder.php
Original file line number Diff line number Diff line change
@@ -5,10 +5,10 @@
use BalajiDharma\LaravelFormBuilder\Facades\FormBuilder;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Http\Request;

class CrudBuilder
{
@@ -76,12 +76,14 @@ public function setIdentifier()
public function setAddtional($addtional)
{
$this->addtional = array_merge($this->addtional, $addtional);

return $this;
}

public function setTitle($title)
{
$this->title = $title;

return $this;
}

@@ -92,18 +94,21 @@ public function setRedirectUrl($redirectUrl = null)
} else {
$this->redirectUrl = url()->current();
}

return $this;
}

public function setDisplayFilters(bool $displayFilters = true)
{
$this->displayFilters = $displayFilters;

return $this;
}

public function setDisplaySearch(bool $displaySearch = true)
{
$this->displaySearch = $displaySearch;

return $this;
}

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

// Get all column names and their types for the table
$tableColumns = Schema::getColumnListing($tableName);

$tableColumns = Schema::getColumns($tableName);
foreach ($columns as $column) {
$attribute = $column['attribute'] ?? null;
$type = $column['type'] ?? 'custom';
$fillable = false;
$primaryKey = false;

if ($attribute && ! isset($column['type']) && in_array($attribute, $tableColumns)) {
$type = DB::getSchemaBuilder()->getColumnType($tableName, $attribute);
$tableColumn = collect($tableColumns)->where('name', $attribute)->first();
if ($attribute && ! isset($column['type']) && $tableColumn) {
$type = $tableColumn['type_name'];
$type = $this->crudHelper->getInputType($type);
$primaryKey = $attribute == $model->getKeyName();
}
@@ -253,7 +257,7 @@ public function applyFilters()
}
}
}
if (!$hasFilters) {
if (! $hasFilters) {
$this->displayFilters = false;
}
}
@@ -306,19 +310,29 @@ public function applySorting()
}
$field = collect($this->fields)->where('attribute', $attribute)->first() ?? null;

if ($field && isset($field['sortable'])) {
if (isset($field['relation'])) {
$relation = $field['relation'];
$relationField = $field['relation_field'] ?? $field['attribute'];
$this->dataProvider->whereHas($relation, function ($q) use ($relationField, $sortOrder) {
$table = $q->getModel()->getTable();
$qualifiedField = "{$table}.{$relationField}";
$q->orderBy($qualifiedField, $sortOrder);
});
} else {
$table = $this->dataProvider->getModel()->getTable();
$this->dataProvider->orderBy("{$table}.{$attribute}", $sortOrder);
}
$this->applyFieldSort($field, $sortOrder, $attribute);
} else {
$field = collect($this->fields)->whereNotNull('defaultSort')->first() ?? null;
if ($field) {
$this->applyFieldSort($field, $field['defaultSort'], $field['attribute']);
}
}
}

public function applyFieldSort($field, $sortOrder, $attribute)
{
if ($field && isset($field['sortable'])) {
if (isset($field['relation'])) {
$relation = $field['relation'];
$relationField = $field['relation_field'] ?? $field['attribute'];
$this->dataProvider->whereHas($relation, function ($q) use ($relationField, $sortOrder) {
$table = $q->getModel()->getTable();
$qualifiedField = "{$table}.{$relationField}";
$q->orderBy($qualifiedField, $sortOrder);
});
} else {
$table = $this->dataProvider->getModel()->getTable();
$this->dataProvider->orderBy("{$table}.{$attribute}", $sortOrder);
}
}
}
@@ -362,23 +376,42 @@ public function buildRoutes($mainRoute = null)
$mainRoute = substr($routeName, 0, strrpos($routeName, '.'));
}

return [
'index' => $this->route($mainRoute.'.index'),
'create' => $this->route($mainRoute.'.create'),
'store' => $this->route($mainRoute.'.store'),
'edit' => function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.edit', $id);
},
'update' => function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.update', $id);
},
'show' => function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.show', $id);
},
'destroy' => function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.destroy', $id);
},
];
$routes = [];
foreach ($this->getActions() as $action => $value) {
if (!$value) {
continue;
}
switch ($action) {
case 'create':
$routes['create'] = $this->route($mainRoute.'.create');
break;
case 'store':
$routes['store'] = $this->route($mainRoute.'.store');
break;
case 'edit':
$routes['edit'] = function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.edit', $id);
};
break;
case 'update':
$routes['update'] = function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.update', $id);
};
break;
case 'show':
$routes['show'] = function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.show', $id);
};
break;
case 'destroy':
$routes['destroy'] = function ($id) use ($mainRoute) {
return $this->route($mainRoute.'.destroy', $id);
};
break;
}
}

return $routes;
}

public function render($view)
@@ -412,7 +445,7 @@ protected function commonRenderData()
'identifier' => $this->identifier,
'redirectUrl' => $this->redirectUrl,
'displaySearch' => $this->displaySearch,
'displayFilters' => $this->displayFilters
'displayFilters' => $this->displayFilters,
];
}

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

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

public function route($name, $parameters = [], $absolute = true)
{
if ($this->redirectUrl)
{
if ($this->redirectUrl) {
return $this->mergeQueryParams(route($name, $parameters), ['_redirect' => $this->redirectUrl]);
} else {
return route($name, $parameters);
@@ -495,18 +527,52 @@ public function mergeQueryParams($url, array $parameters = [])

// Remove null/empty parameters
$query = array_filter($query, function ($value) {
return !is_null($value) && $value !== '';
return ! is_null($value) && $value !== '';
});

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

// Return URL with merged query parameters
return $query
? $baseUrl . '?' . http_build_query($query)
? $baseUrl.'?'.http_build_query($query)
: $baseUrl;
} catch (\Exception $e) {
return $url;
}
}

public function getActions()
{
$default = [
'index' => true,
'create' => true,
'store' => true,
'edit' => true,
'update' => true,
'show' => true,
'destroy' => true,
];
return array_merge($default, $this->actions ?? []);
}


public function setActions(array $actions)
{
$this->actions = $actions;
}


public function getAction($action)
{
$actions = $this->getActions();
return $actions[$action] ?? false;
}

public function setAction($action, $value)
{
$actions = $this->getActions();
$actions[$action] = $value;
$this->setActions($actions);
}
}
3 changes: 2 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@ function crud(CrudBuilder $crud, $view = 'list')
function crudRedirect($name, $message)
{
$redirectUrl = request()->input('_redirect', route($name));

return redirect($redirectUrl)->withMessage(__($message));
}

}
}