diff --git a/app/Http/Livewire/Tables/LibraryItemTable.php b/app/Http/Livewire/Tables/LibraryItemTable.php
index d81e2255..2c0abec2 100644
--- a/app/Http/Livewire/Tables/LibraryItemTable.php
+++ b/app/Http/Livewire/Tables/LibraryItemTable.php
@@ -2,14 +2,17 @@
namespace App\Http\Livewire\Tables;
+use App\Models\Library;
use App\Models\LibraryItem;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
+use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
class LibraryItemTable extends DataTableComponent
{
+ public string $currentTab;
protected $model = LibraryItem::class;
public function configure(): void
@@ -19,6 +22,35 @@ class LibraryItemTable extends DataTableComponent
->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
{
return [
@@ -54,6 +86,8 @@ class LibraryItemTable extends DataTableComponent
public function builder(): Builder
{
return LibraryItem::query()
+ ->when($this->currentTab !== 'Alle', fn($query) => $query->whereHas('libraries',
+ fn($query) => $query->where('libraries.name', $this->currentTab)))
->withCount([
'lecturer',
]);
diff --git a/app/Models/LibraryItem.php b/app/Models/LibraryItem.php
index bc6a210d..bf513037 100644
--- a/app/Models/LibraryItem.php
+++ b/app/Models/LibraryItem.php
@@ -64,7 +64,7 @@ class LibraryItem extends Model implements HasMedia, Sortable
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);
}
diff --git a/resources/views/livewire/frontend/library.blade.php b/resources/views/livewire/frontend/library.blade.php
index c5a92d51..d0bb2f79 100644
--- a/resources/views/livewire/frontend/library.blade.php
+++ b/resources/views/livewire/frontend/library.blade.php
@@ -38,7 +38,7 @@