Script erstellt
This commit is contained in:
154
app/Http/Controllers/ManufacturerController.php
Normal file
154
app/Http/Controllers/ManufacturerController.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Manufacturer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ManufacturerController extends Controller
|
||||
{
|
||||
/**
|
||||
* Zeigt alle Hersteller an
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$search = $request->get('search');
|
||||
|
||||
$query = Manufacturer::withCount('nailPolishes');
|
||||
|
||||
if ($search) {
|
||||
$query->search($search);
|
||||
}
|
||||
|
||||
$manufacturers = $query->orderBy('name')->paginate(20);
|
||||
|
||||
return view('manufacturers.index', compact('manufacturers', 'search'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt das Formular zum Erstellen eines neuen Herstellers
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('manufacturers.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert einen neuen Hersteller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255|unique:manufacturers',
|
||||
'description' => 'nullable|string|max:1000',
|
||||
'website' => 'nullable|url|max:255',
|
||||
'country' => 'nullable|string|max:100',
|
||||
]);
|
||||
|
||||
try {
|
||||
$manufacturer = Manufacturer::create([
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'website' => $request->website,
|
||||
'country' => $request->country,
|
||||
]);
|
||||
|
||||
return redirect()->route('manufacturers.index')
|
||||
->with('success', "Hersteller '{$manufacturer->name}' wurde erfolgreich erstellt!");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Log::error("Fehler beim Erstellen des Herstellers: " . $e->getMessage());
|
||||
return back()->with('error', 'Fehler beim Erstellen des Herstellers. Bitte versuchen Sie es erneut.')
|
||||
->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt einen Hersteller an
|
||||
*/
|
||||
public function show(Manufacturer $manufacturer)
|
||||
{
|
||||
$manufacturer->load(['nailPolishes' => function($query) {
|
||||
$query->orderBy('name');
|
||||
}]);
|
||||
|
||||
return view('manufacturers.show', compact('manufacturer'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt das Formular zum Bearbeiten eines Herstellers
|
||||
*/
|
||||
public function edit(Manufacturer $manufacturer)
|
||||
{
|
||||
return view('manufacturers.edit', compact('manufacturer'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert einen Hersteller
|
||||
*/
|
||||
public function update(Request $request, Manufacturer $manufacturer)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255|unique:manufacturers,name,' . $manufacturer->id,
|
||||
'description' => 'nullable|string|max:1000',
|
||||
'website' => 'nullable|url|max:255',
|
||||
'country' => 'nullable|string|max:100',
|
||||
]);
|
||||
|
||||
try {
|
||||
$manufacturer->update([
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'website' => $request->website,
|
||||
'country' => $request->country,
|
||||
]);
|
||||
|
||||
return redirect()->route('manufacturers.index')
|
||||
->with('success', "Hersteller '{$manufacturer->name}' wurde erfolgreich aktualisiert!");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Log::error("Fehler beim Aktualisieren des Herstellers: " . $e->getMessage());
|
||||
return back()->with('error', 'Fehler beim Aktualisieren des Herstellers. Bitte versuchen Sie es erneut.')
|
||||
->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht einen Hersteller
|
||||
*/
|
||||
public function destroy(Manufacturer $manufacturer)
|
||||
{
|
||||
try {
|
||||
// Prüfe, ob der Hersteller noch Nagellacke hat
|
||||
if ($manufacturer->hasNailPolishes()) {
|
||||
return back()->with('error', "Hersteller '{$manufacturer->name}' kann nicht gelöscht werden, da noch Nagellacke zugeordnet sind.");
|
||||
}
|
||||
|
||||
$name = $manufacturer->name;
|
||||
$manufacturer->delete();
|
||||
|
||||
return redirect()->route('manufacturers.index')
|
||||
->with('success', "Hersteller '{$name}' wurde erfolgreich gelöscht!");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Log::error("Fehler beim Löschen des Herstellers: " . $e->getMessage());
|
||||
return back()->with('error', 'Fehler beim Löschen des Herstellers. Bitte versuchen Sie es erneut.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API-Endpoint für AJAX-Suche nach Herstellern
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$search = $request->get('q');
|
||||
|
||||
$manufacturers = Manufacturer::where('name', 'like', "%{$search}%")
|
||||
->orderBy('name')
|
||||
->limit(10)
|
||||
->get(['id', 'name']);
|
||||
|
||||
return response()->json($manufacturers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user