Add Decks CRUD

main
Alex 2 years ago
parent d9bef7a420
commit 8f6926c1cc

@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DeckController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('decks');
}
/**
* Display a listing of the Deck-Images.
*
* @return \Illuminate\Http\Response
*/
public function viewImage()
{
return view('view_image');
}
}

@ -0,0 +1,101 @@
<?php
namespace App\Http\Livewire;
use App\Models\Deck;
use Livewire\Component;
use Livewire\WithPagination;
class DeckForm extends Component
{
use WithPagination;
public $name;
public $uname;
public $description;
public $card_count;
public $cover_path;
public $backside_path;
public $author;
public $publisher;
public $type;
public $deck_id;
protected $rules = [
'name' => ['required'],
'uname' => ['required', 'unique:App\Models\Deck', 'string', 'size:4'],
'description' => ['nullable'],
'card_count' => ['required', 'numeric'],
'cover_path' => ['required','image'],
'backside_path' => ['required','image'],
'author' => ['nullable'],
'publisher' => ['nullable'],
'type' => ['nullable'],
];
public function storeDeck()
{
$this->validate();
$deck = Deck::create([
'name' => $this->name,
'uname' =>$this->uname,
'description' => $this->description,
'card_count' => $this->card_count,
'cover_path' => $this->cover_path,
'backside_path' => $this->backside_path,
'author' => $this->author,
'publisher' => $this->publisher,
'type' => $this->type
]);
$this->reset();
}
public function edit($id)
{
$deck = Deck::find($id);
$this->deck_id = $deck->id;
$this->name = $deck->name;
$this->uname = $deck->uname;
$this->description = $deck->description;
$this->card_count = $deck->card_count;
$this->cover_path = $deck->cover_path;
$this->backside_path = $deck->backside_path;
$this->author = $deck->author;
$this->publisher = $deck->publisher;
$this->type = $deck->type;
}
public function update()
{
$deck = Deck::updateOrCreate(
[
'id' => $this->deck_id,
],
[
'name' => $this->name,
'uname' =>$this->uname,
'description' => $this->description,
'card_count' => $this->card_count,
'cover_path' => $this->cover_path,
'backside_path' => $this->backside_path,
'author' => $this->author,
'publisher' => $this->publisher,
'type' => $this->type
],
);
$this->reset();
}
public function destroy($id)
{
Deck::destroy($id);
}
public function render()
{
return view('livewire.deck-form', ['decks' => Deck::latest()->paginate(10)]);
}
}

@ -9,6 +9,18 @@ class Deck extends Model
{
use HasFactory;
protected $fillable = [
'name',
'uname',
'describtion',
'card_count',
'cover_path',
'backside_path',
'author',
'publisher',
'type'
];
/**
* The tags that belong to the Deck
*

@ -14,10 +14,13 @@ return new class extends Migration
Schema::create('decks', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('uname');
$table->string('description')->nullable();
$table->integer('card_count');
$table->string('cover_path');
$table->string('backside_path');
$table->string('author')->nullable();
$table->string('publisher')->nullable();
$table->string('type')->nullable();
$table->timestamps();
});

@ -0,0 +1,22 @@
<x-layout>
<x-slot name="scripts">
<script>
</script>
</x-slot>
<x-slot name="title">Decks</x-slot>
<x-slot name="content">
<div class="container px-4 mx-auto">
<div class="py-12">
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
<div class="overflow-hidden bg-white shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<livewire:deck-form />
</div>
</div>
</div>
</div>
</div> <!-- Ende container -->
</x-slot>
</x-layout>

@ -0,0 +1,158 @@
<div>
<h4 class="mb-4 text-2xl font-bold">Deck </h4>
<div>
<div class="container mx-auto">
<form method="POST" wire:submit.prevent="storeDeck">
@csrf
<div>
<label for="name">Name</label>
<input type="text" wire:model.lazy="name" class="w-full py-2 rounded">
@error('name')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Einzigartige Kennung (4 Stellen) </label>
<textarea wire:model.lazy="uname" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('uname')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Beschreibung </label>
<textarea wire:model.lazy="description" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('description')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Anzahl Karten im Deck </label>
<textarea wire:model.lazy="card_count" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('card_count')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Pfad zu Cover Bild </label>
<textarea wire:model.lazy="cover_path" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('cover_path')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Pfad zu Karten-Rückseite </label>
<textarea wire:model.lazy="backside_path" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('backside_path')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Author</label>
<textarea wire:model.lazy="author" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('author')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Publisher</label>
<textarea wire:model.lazy="publisher" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('publisher')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<div class="mt-8">
<label class="block mb-2 text-xl">Deck Typ</label>
<textarea wire:model.lazy="type" rows="3" cols="20" class="w-full rounded">
</textarea>
@error('type')
<span class="text-red-600">{{ $message }}</span>
@enderror
</div>
<button type="submit" class="px-4 py-2 mt-4 text-white bg-blue-600 rounded">
Submit
</button>
<button type="submit" wire:click="update" class="px-4 py-2 text-white bg-indigo-600 rounded">
Update
</button>
</form>
</div>
<div class="flex flex-col mt-8">
<div class="py-2">
<div
class="min-w-full border-b border-gray-200 shadow">
<table class="min-w-full">
<thead>
<tr>
<th class="px-6 py-3 text-left text-gray-500 border-b border-gray-200 bg-gray-50">
Id
</th>
<th class="px-6 py-3 text-left text-gray-500 border-b border-gray-200 bg-gray-50">
Name
</th>
<th class="px-6 py-3 text-left text-gray-500 border-b border-gray-200 bg-gray-50">
U-Name
</th>
<th class="px-6 py-3 text-left text-gray-500 border-b border-gray-200 bg-gray-50">
Edit
</th>
<th class="px-6 py-3 text-left text-gray-500 border-b border-gray-200 bg-gray-50">
Delete
</th>
</tr>
</thead>
<tbody class="bg-white">
@foreach($decks as $deck)
<tr>
<td class="px-6 py-4 border-b border-gray-200">
<div class="flex items-center">
<div class="ml-4">
<div class="text-sm text-gray-900">
{{ $deck->id }}
</div>
</div>
</div>
</td>
<td class="px-6 py-4 border-b border-gray-200">
<div class="text-sm text-gray-900">
{{ $deck->name }}
</div>
</td>
<td class="px-6 py-4 border-b border-gray-200">
<div class="text-sm text-gray-900">
{{ $deck->uname }}
</div>
</td>
<td class="px-6 py-4 border-b border-gray-200">
<button wire:click="edit({{ $deck->id }})" class="px-4 py-2 text-white bg-blue-600">
Edit
</button>
</td>
<td class="px-6 py-4 text-sm text-gray-500 border-b border-gray-200">
<button wire:click="destroy({{ $deck->id }})"
class="px-4 py-2 text-white bg-red-600">
Delete
</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
{{ $decks->links() }}
</div>
</div>
</div>
</div>
Loading…
Cancel
Save