library items parent_id

This commit is contained in:
Benjamin Takats
2023-01-19 14:59:29 +01:00
parent b1073919df
commit d1d98b6c31
7 changed files with 77 additions and 12 deletions

View File

@@ -26,6 +26,7 @@ class LibraryTable extends Component
}
$libraries = \App\Models\Library::query()
->whereNull('parent_id')
->where('is_public', $shouldBePublic)
->get();
$tabs = collect([

View File

@@ -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',
])

View File

@@ -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');
}
}

View File

@@ -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(),
];
}

View File

@@ -42,6 +42,11 @@ class LibraryItem extends Resource
'name',
];
public static $with = [
'lecturer',
'tags',
];
public static function label()
{
return __('Library Item');

View File

@@ -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);

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
* @return void
*/
public function up()
{
Schema::table('libraries', function (Blueprint $table) {
$table->unsignedBigInteger('parent_id')
->nullable()
->after('id');
});
}
/**
* Reverse the migrations.
* @return void
*/
public function down()
{
Schema::table('libraries', function (Blueprint $table) {
//
});
}
};