mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
filter lib
This commit is contained in:
@@ -2,14 +2,17 @@
|
|||||||
|
|
||||||
namespace App\Http\Livewire\Tables;
|
namespace App\Http\Livewire\Tables;
|
||||||
|
|
||||||
|
use App\Models\Library;
|
||||||
use App\Models\LibraryItem;
|
use App\Models\LibraryItem;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||||
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
|
||||||
|
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||||
|
|
||||||
class LibraryItemTable extends DataTableComponent
|
class LibraryItemTable extends DataTableComponent
|
||||||
{
|
{
|
||||||
|
public string $currentTab;
|
||||||
protected $model = LibraryItem::class;
|
protected $model = LibraryItem::class;
|
||||||
|
|
||||||
public function configure(): void
|
public function configure(): void
|
||||||
@@ -19,6 +22,35 @@ class LibraryItemTable extends DataTableComponent
|
|||||||
->setDefaultSort('order_column', 'asc');
|
->setDefaultSort('order_column', 'asc');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function filters(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
SelectFilter::make('Bibliothek')
|
||||||
|
->options(
|
||||||
|
Library::query()
|
||||||
|
->get()
|
||||||
|
->prepend(new Library(['name' => 'Alle']))
|
||||||
|
->pluck('name', 'name')
|
||||||
|
->toArray(),
|
||||||
|
)
|
||||||
|
->filter(function (Builder $builder, string $value) {
|
||||||
|
if ($value === 'Alle') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (str($value)->contains(',')) {
|
||||||
|
$builder
|
||||||
|
->whereHas('libraries',
|
||||||
|
function ($query) use ($value) {
|
||||||
|
$query->whereIn('libraries.name', str($value)->explode(','));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$builder->whereHas('libraries',
|
||||||
|
fn($query) => $query->where('libraries.name', 'ilike', "%$value%"));
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function columns(): array
|
public function columns(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@@ -54,6 +86,8 @@ class LibraryItemTable extends DataTableComponent
|
|||||||
public function builder(): Builder
|
public function builder(): Builder
|
||||||
{
|
{
|
||||||
return LibraryItem::query()
|
return LibraryItem::query()
|
||||||
|
->when($this->currentTab !== 'Alle', fn($query) => $query->whereHas('libraries',
|
||||||
|
fn($query) => $query->where('libraries.name', $this->currentTab)))
|
||||||
->withCount([
|
->withCount([
|
||||||
'lecturer',
|
'lecturer',
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class LibraryItem extends Model implements HasMedia, Sortable
|
|||||||
return $this->belongsTo(Lecturer::class);
|
return $this->belongsTo(Lecturer::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function library(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
public function libraries(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Library::class);
|
return $this->belongsToMany(Library::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
|
|
||||||
<livewire:frontend.search-by-tag-in-library/>
|
<livewire:frontend.search-by-tag-in-library/>
|
||||||
<div class="my-12">
|
<div class="my-12">
|
||||||
<livewire:tables.library-item-table/>
|
<livewire:tables.library-item-table :currentTab="$currentTab"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Reference in New Issue
Block a user