From d1d98b6c318cae6506e4a04ed21b90479548bea4 Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Thu, 19 Jan 2023 14:59:29 +0100 Subject: [PATCH] library items parent_id --- app/Http/Livewire/Library/LibraryTable.php | 1 + app/Http/Livewire/Tables/LibraryItemTable.php | 22 ++++++++++--- app/Models/Library.php | 6 ++++ app/Nova/Library.php | 22 ++++++++----- app/Nova/LibraryItem.php | 5 +++ app/Providers/AppServiceProvider.php | 2 +- ...add_parent_id_field_to_libraries_table.php | 31 +++++++++++++++++++ 7 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 database/migrations/2023_01_19_133503_add_parent_id_field_to_libraries_table.php diff --git a/app/Http/Livewire/Library/LibraryTable.php b/app/Http/Livewire/Library/LibraryTable.php index 24bbdf39..e460c39b 100644 --- a/app/Http/Livewire/Library/LibraryTable.php +++ b/app/Http/Livewire/Library/LibraryTable.php @@ -26,6 +26,7 @@ class LibraryTable extends Component } $libraries = \App\Models\Library::query() + ->whereNull('parent_id') ->where('is_public', $shouldBePublic) ->get(); $tabs = collect([ diff --git a/app/Http/Livewire/Tables/LibraryItemTable.php b/app/Http/Livewire/Tables/LibraryItemTable.php index 2e177a4c..feb1e4f1 100644 --- a/app/Http/Livewire/Tables/LibraryItemTable.php +++ b/app/Http/Livewire/Tables/LibraryItemTable.php @@ -153,8 +153,14 @@ class LibraryItemTable extends DataTableComponent public function builder(): Builder { $shouldBePublic = request() - ->route() - ->getName() !== 'library.table.lecturer'; + ?->route() + ?->getName() !== 'library.table.lecturer'; + + if ($this->currentTab !== '*') { + $parentLibrary = Library::query() + ->where('name', $this->currentTab) + ->first(); + } return LibraryItem::query() ->with([ @@ -162,8 +168,16 @@ class LibraryItemTable extends DataTableComponent 'tags', ]) ->whereHas('libraries', fn($query) => $query->where('libraries.is_public', $shouldBePublic)) - ->when($this->currentTab !== '*', fn($query) => $query->whereHas('libraries', - fn($query) => $query->where('libraries.name', $this->currentTab))) + ->when($this->currentTab !== '*', fn($query) => $query + ->whereHas('libraries', + fn($query) => $query + ->where('libraries.name', $this->currentTab) + ) + ->orWhereHas('libraries', + fn($query) => $query + ->where('libraries.parent_id', $parentLibrary->id) + ) + ) ->withCount([ 'lecturer', ]) diff --git a/app/Models/Library.php b/app/Models/Library.php index 2a204cac..dfdb5dc8 100644 --- a/app/Models/Library.php +++ b/app/Models/Library.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; class Library extends Model { @@ -44,4 +45,9 @@ class Library extends Model { return $this->belongsToMany(LibraryItem::class); } + + public function parent() : BelongsTo + { + return $this->belongsTo(__CLASS__, 'parent_id'); + } } diff --git a/app/Nova/Library.php b/app/Nova/Library.php index 05e414bb..5074cc89 100644 --- a/app/Nova/Library.php +++ b/app/Nova/Library.php @@ -26,12 +26,6 @@ class Library extends Resource * @var string */ public static $title = 'name'; - - public static function label() - { - return __('Library'); - } - /** * The columns that should be searched. * @var array @@ -41,6 +35,15 @@ class Library extends Resource 'name', ]; + public static $with = [ + 'createdBy', + ]; + + public static function label() + { + return __('Library'); + } + public static function afterCreate(NovaRequest $request, Model $model) { \App\Models\User::find(1) @@ -68,6 +71,10 @@ class Library extends Resource ID::make() ->sortable(), + BelongsTo::make(__('Parent'), 'parent', __CLASS__) + ->searchable() + ->nullable(), + Text::make('Name') ->rules('required', 'string'), @@ -86,7 +93,8 @@ class Library extends Resource return $request->user() ->hasRole('super-admin'); }) - ->searchable()->withSubtitles(), + ->searchable() + ->withSubtitles(), ]; } diff --git a/app/Nova/LibraryItem.php b/app/Nova/LibraryItem.php index 52926a81..50400a38 100644 --- a/app/Nova/LibraryItem.php +++ b/app/Nova/LibraryItem.php @@ -42,6 +42,11 @@ class LibraryItem extends Resource 'name', ]; + public static $with = [ + 'lecturer', + 'tags', + ]; + public static function label() { return __('Library Item'); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4c3c3ce6..87542eca 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -30,7 +30,7 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - //Model::preventLazyLoading(); + Model::preventLazyLoading(app()->environment('local')); Stringable::macro('initials', function () { $words = preg_split("/\s+/", $this); diff --git a/database/migrations/2023_01_19_133503_add_parent_id_field_to_libraries_table.php b/database/migrations/2023_01_19_133503_add_parent_id_field_to_libraries_table.php new file mode 100644 index 00000000..971c23be --- /dev/null +++ b/database/migrations/2023_01_19_133503_add_parent_id_field_to_libraries_table.php @@ -0,0 +1,31 @@ +unsignedBigInteger('parent_id') + ->nullable() + ->after('id'); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + Schema::table('libraries', function (Blueprint $table) { + // + }); + } +};