Skip to content

Commit

Permalink
fix: paginate & improvment
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkhal committed Feb 4, 2023
1 parent 2f6be70 commit 778b0f6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 3 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ class UserTable extends Inertable
}
}
```

Add hidden response

```php
protected $hiddens = [
'office'
];
```

The response will include `office` object in response to inertable props.

Hidden response also accept relatoin, for example if you have `HasMany` relation from user to posts

```php
protected $hiddens = [
'user.posts'
];
```

That's will return hidden user posts inside inertable props.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.0",
"illuminate/support": "^8.62|^9.0.0",
"inertiajs/inertia-laravel": "^0.6.2"
"inertiajs/inertia-laravel": "^0.6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.1",
Expand Down
45 changes: 45 additions & 0 deletions src/Commands/MakeDatatableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Rizkhal\Datatable\Commands;

use Illuminate\Console\GeneratorCommand;

class MakeDatatableCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:datatable';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Datatable class';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/datatable.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Datatable';
}
}
31 changes: 31 additions & 0 deletions src/Commands/stubs/datatable.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DummyNamespace;

use Rizkhal\Datatable\Column;
use Rizkhal\Datatable\Datatable;
use Illuminate\Database\Eloquent\Builder;

class DummyClass extends Datatable
{
public function query(): Builder
{
// ...
}

public function columns(): array
{
return [
// ...
];
}

public function fields(): array
{
return [
// ...
];
}
}
13 changes: 12 additions & 1 deletion src/Inertable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rizkhal\Inertable\Concerns\WithQuery;
use Rizkhal\Inertable\Concerns\WithSorting;
use Rizkhal\Inertable\Utils\ColumnAttributes;
use Rizkhal\Inertable\View\Column;

abstract class Inertable implements WithQuery, WithColumn
{
Expand Down Expand Up @@ -39,6 +40,16 @@ public function columnsMerged(): array
return array_merge([Column::make($this->getKey())], $this->columns());
}

if (property_exists($this, 'hiddens')) {
$tmp = [];

foreach ($this->hiddens as $key => $value) {
$tmp[] = Column::make($value);
}

return array_merge([Column::make('id')], $tmp, $this->columns());
}

return array_merge([Column::make('id')], $this->columns());
}

Expand Down Expand Up @@ -77,7 +88,7 @@ public function applyInertable(Request $request): LengthAwarePaginator

$query = $this->applySearchFilter($request, $query);

return $query->paginate($this->perPage(), ['*'], $request->get('table') ?: 'inertable')->withQueryString()->through(fn ($model) => $this->through($model));
return $query->paginate($this->perPage())->withQueryString()->through(fn ($model) => $this->through($model));
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Column.php → src/View/Column.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Rizkhal\Inertable;
namespace Rizkhal\Inertable\View;

use Closure;
use Illuminate\Support\Str;
use Rizkhal\Inertable\Enums\ColumnType;

class Column
{
public bool $na = false;

public bool $blank = false;

public bool $sortable = false;
Expand Down Expand Up @@ -133,4 +135,11 @@ public function getFormatCallback(): callable|null
{
return $this->formatCallback;
}

public function nullable()
{
$this->na = true;

return $this;
}
}

0 comments on commit 778b0f6

Please sign in to comment.