mirror of
https://github.com/anna-sara/filament_inventory
synced 2025-10-26 23:27:13 +01:00
Item: migration, model, filament
This commit is contained in:
parent
75c877b007
commit
b04c657f05
7 changed files with 279 additions and 0 deletions
134
app/Filament/Resources/ItemResource.php
Normal file
134
app/Filament/Resources/ItemResource.php
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Filament\Resources\ItemResource\Pages;
|
||||
use App\Filament\Resources\ItemResource\RelationManagers;
|
||||
use App\Models\Item;
|
||||
use App\Models\User;
|
||||
use App\Models\Type;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Filament\Forms\Components\Select;
|
||||
|
||||
|
||||
class ItemResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Item::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-archive-box';
|
||||
|
||||
protected static ?string $modelLabel = 'Items';
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\FileUpload::make('image')
|
||||
->label('Bild')
|
||||
->minSize(512)
|
||||
->maxSize(1024)
|
||||
->columnSpan('full')
|
||||
->image(),
|
||||
Forms\Components\TextInput::make('desc')
|
||||
->label('Beskrivning')
|
||||
->maxLength(255)
|
||||
->default(null),
|
||||
Forms\Components\DatePicker::make('acquisition_date')
|
||||
->label('Inköpsdatum'),
|
||||
Forms\Components\TextInput::make('quantity')
|
||||
->label('Antal')
|
||||
->numeric()
|
||||
->minValue(0)
|
||||
->maxValue(1000)
|
||||
->default(0),
|
||||
Forms\Components\Select::make('type_id')
|
||||
->label('Typ')
|
||||
->options(Type::all()->pluck('name', 'id'))
|
||||
->required(),
|
||||
Forms\Components\TextInput::make('cost')
|
||||
->label('Kostnad')
|
||||
->default(null),
|
||||
Forms\Components\Toggle::make('can_be_loaned')
|
||||
->label('Kan lånas ut'),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('desc')
|
||||
->label('Beskrivning')
|
||||
->searchable(),
|
||||
Tables\Columns\TextColumn::make('acquisition_date')
|
||||
->label('Inköpsdatum')
|
||||
->date()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('quantity')
|
||||
->label('Antal')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('type.name')
|
||||
->label('Typ')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('cost')
|
||||
->label('Kostnad')
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\ImageColumn::make('image')
|
||||
->label('Bild'),
|
||||
Tables\Columns\IconColumn::make('can_be_loaned')
|
||||
->label('Kan bli utlånad')
|
||||
->trueIcon('heroicon-o-check-badge')
|
||||
->falseIcon('heroicon-o-x-mark')
|
||||
->trueColor('success')
|
||||
->falseColor('danger'),
|
||||
//Tables\Columns\TextColumn::make('reserveditems.reserved')
|
||||
// ->label('Ska returneras'),
|
||||
//Tables\Columns\TextColumn::make('reserveditems.name')
|
||||
// ->label('Utlånad till')
|
||||
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListItems::route('/'),
|
||||
'create' => Pages\CreateItem::route('/create'),
|
||||
'edit' => Pages\EditItem::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function canViewAny(): bool
|
||||
{
|
||||
return auth()->user()->is_admin==true;
|
||||
}
|
||||
|
||||
}
|
||||
17
app/Filament/Resources/ItemResource/Pages/CreateItem.php
Normal file
17
app/Filament/Resources/ItemResource/Pages/CreateItem.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ItemResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ItemResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateItem extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ItemResource::class;
|
||||
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
return $this->getResource()::getUrl('index');
|
||||
}
|
||||
}
|
||||
26
app/Filament/Resources/ItemResource/Pages/EditItem.php
Normal file
26
app/Filament/Resources/ItemResource/Pages/EditItem.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ItemResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ItemResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditItem extends EditRecord
|
||||
{
|
||||
protected static string $resource = ItemResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
return $this->getResource()::getUrl('index');
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/ItemResource/Pages/ListItems.php
Normal file
19
app/Filament/Resources/ItemResource/Pages/ListItems.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ItemResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ItemResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListItems extends ListRecords
|
||||
{
|
||||
protected static string $resource = ItemResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
10
app/Http/Controllers/ItemController.php
Normal file
10
app/Http/Controllers/ItemController.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ItemController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
38
app/Models/Item.php
Normal file
38
app/Models/Item.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
use App\Models\User;
|
||||
use App\Models\Type;
|
||||
use App\Models\Reserveditem;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Item extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'desc',
|
||||
'image',
|
||||
'acquisition_date',
|
||||
'quantity',
|
||||
'cost',
|
||||
'can_be_loaned',
|
||||
'type_id'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Get the type of the item
|
||||
*/
|
||||
public function type(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Type::class);
|
||||
}
|
||||
|
||||
public function reserveditem()
|
||||
{
|
||||
return $this->hasOne(Reserveditem::class);
|
||||
}
|
||||
}
|
||||
35
database/migrations/2025_01_15_160621_create_items_table.php
Normal file
35
database/migrations/2025_01_15_160621_create_items_table.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('items', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('desc')->nullable();
|
||||
$table->date('acquisition_date')->nullable();
|
||||
$table->string('image')->nullable();
|
||||
$table->integer('type_id')->nullable();
|
||||
$table->integer('quantity')->nullable();
|
||||
$table->string('cost')->nullable();
|
||||
$table->boolean('can_be_loaned')->default(false);
|
||||
$table->boolean('reserved')->default(false);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('items');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue