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() $libraries = \App\Models\Library::query()
->whereNull('parent_id')
->where('is_public', $shouldBePublic) ->where('is_public', $shouldBePublic)
->get(); ->get();
$tabs = collect([ $tabs = collect([

View File

@@ -153,8 +153,14 @@ class LibraryItemTable extends DataTableComponent
public function builder(): Builder public function builder(): Builder
{ {
$shouldBePublic = request() $shouldBePublic = request()
->route() ?->route()
->getName() !== 'library.table.lecturer'; ?->getName() !== 'library.table.lecturer';
if ($this->currentTab !== '*') {
$parentLibrary = Library::query()
->where('name', $this->currentTab)
->first();
}
return LibraryItem::query() return LibraryItem::query()
->with([ ->with([
@@ -162,8 +168,16 @@ class LibraryItemTable extends DataTableComponent
'tags', 'tags',
]) ])
->whereHas('libraries', fn($query) => $query->where('libraries.is_public', $shouldBePublic)) ->whereHas('libraries', fn($query) => $query->where('libraries.is_public', $shouldBePublic))
->when($this->currentTab !== '*', fn($query) => $query->whereHas('libraries', ->when($this->currentTab !== '*', fn($query) => $query
fn($query) => $query->where('libraries.name', $this->currentTab))) ->whereHas('libraries',
fn($query) => $query
->where('libraries.name', $this->currentTab)
)
->orWhereHas('libraries',
fn($query) => $query
->where('libraries.parent_id', $parentLibrary->id)
)
)
->withCount([ ->withCount([
'lecturer', 'lecturer',
]) ])

View File

@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Library extends Model class Library extends Model
{ {
@@ -44,4 +45,9 @@ class Library extends Model
{ {
return $this->belongsToMany(LibraryItem::class); 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 * @var string
*/ */
public static $title = 'name'; public static $title = 'name';
public static function label()
{
return __('Library');
}
/** /**
* The columns that should be searched. * The columns that should be searched.
* @var array * @var array
@@ -41,6 +35,15 @@ class Library extends Resource
'name', 'name',
]; ];
public static $with = [
'createdBy',
];
public static function label()
{
return __('Library');
}
public static function afterCreate(NovaRequest $request, Model $model) public static function afterCreate(NovaRequest $request, Model $model)
{ {
\App\Models\User::find(1) \App\Models\User::find(1)
@@ -68,6 +71,10 @@ class Library extends Resource
ID::make() ID::make()
->sortable(), ->sortable(),
BelongsTo::make(__('Parent'), 'parent', __CLASS__)
->searchable()
->nullable(),
Text::make('Name') Text::make('Name')
->rules('required', 'string'), ->rules('required', 'string'),
@@ -86,7 +93,8 @@ class Library extends Resource
return $request->user() return $request->user()
->hasRole('super-admin'); ->hasRole('super-admin');
}) })
->searchable()->withSubtitles(), ->searchable()
->withSubtitles(),
]; ];
} }

View File

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

View File

@@ -30,7 +30,7 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
//Model::preventLazyLoading(); Model::preventLazyLoading(app()->environment('local'));
Stringable::macro('initials', function () { Stringable::macro('initials', function () {
$words = preg_split("/\s+/", $this); $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) {
//
});
}
};