140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Filament;
|
|
use App\Models\Color;
|
|
use App\Models\FilamentType;
|
|
use App\Models\Manufacturer;
|
|
|
|
class FilamentController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// show all filaments
|
|
$filaments = Filament::all();
|
|
$manufacturers = Manufacturer::all();
|
|
return view('filaments.index', ['filaments' => $filaments, 'manufacturers' => $manufacturers]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
// create a new filament
|
|
$manufacturers = Manufacturer::all();
|
|
$types = FilamentType::all();
|
|
$colors = Color::all();
|
|
return view('filaments.create', compact('manufacturers', 'types', 'colors'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// store a new filament
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'color_id' => 'required|exists:colors,id',
|
|
'type_id' => 'required|exists:filament_types,id',
|
|
'manufacturer_id' => 'required|exists:manufacturers,id',
|
|
'weight' => 'required|numeric',
|
|
'price' => 'nullable|decimal:2',
|
|
'diameter' => 'required|decimal:2'
|
|
]);
|
|
|
|
$filament = new Filament();
|
|
$filament->name = $request->name;
|
|
$filament->description = $request->description;
|
|
$filament->color_id = $request->color_id;
|
|
$filament->type_id = $request->type_id;
|
|
$filament->manufacturer_id = $request->manufacturer_id;
|
|
$filament->weight = $request->weight;
|
|
$filament->price = $request->price;
|
|
$filament->diameter = $request->diameter;
|
|
$filament->save();
|
|
|
|
ManufacturerController::refresh();
|
|
return redirect()->route('filaments.index')->with('success', 'Filament created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
// show a specific filament
|
|
$filament = Filament::find($id);
|
|
$manufacturer = Manufacturer::find($filament->manufacturer_id);
|
|
$type = FilamentType::find($filament->type_id);
|
|
$color = Color::find($filament->color_id);
|
|
return view('filaments.show', compact('filament', 'manufacturer', 'type', 'color'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
// edit a specific filament
|
|
$filament = Filament::find($id);
|
|
$manufacturers = Manufacturer::all();
|
|
$types = FilamentType::all();
|
|
$colors = Color::all();
|
|
return view('filaments.edit', compact('filament', 'manufacturers', 'types', 'colors'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
// update a specific filament
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'color_id' => 'required|exists:colors,id',
|
|
'type_id' => 'required|exists:filament_types,id',
|
|
'manufacturer_id' => 'required|exists:manufacturers,id',
|
|
'weight' => 'required|numeric',
|
|
'price' => 'nullable|decimal:2',
|
|
'diameter' => 'required|decimal:2'
|
|
]);
|
|
|
|
$filament = Filament::find($id);
|
|
$filament->name = $request->name;
|
|
$filament->description = $request->description;
|
|
$filament->color_id = $request->color_id;
|
|
$filament->type_id = $request->type_id;
|
|
$filament->manufacturer_id = $request->manufacturer_id;
|
|
$filament->weight = $request->weight;
|
|
$filament->price = $request->price;
|
|
$filament->diameter = $request->diameter;
|
|
$filament->save();
|
|
|
|
ManufacturerController::refresh();
|
|
|
|
return redirect()->route('filaments.show', $filament->id)->with('success', 'Filament updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
// delete a specific filament
|
|
$filament = Filament::find($id);
|
|
$filament->delete();
|
|
|
|
ManufacturerController::refresh();
|
|
return redirect()->route('filaments.index')->with('success', 'Filament deleted successfully.');
|
|
}
|
|
}
|