【发布时间】:2022-01-05 10:09:22
【问题描述】:
我正在尝试向我的应用程序添加一个删除功能,其中将显示一个库存列表,如果您愿意,您可以删除一个项目。但是,我不知道我哪里出错了,因为它确实说支持删除。
这是我的路由器:
Route::delete('/inventories/{inventory}', [\App\Http\Controllers\InventoryController::class, 'destroy'])->name('inventories.destroy');
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Log;
class InventoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$inventories = Inventory::all();
return view('pages.inventories',[
"inventories" => $inventories
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function create(): \Illuminate\Contracts\View\View
{
return view('pages.inventories.create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Redirector
*/
public function store(Request $request): Redirector
{
$validated = $request->validate([
'title'=> 'required|string',
'description'=> 'required|string|max:300',
'price' => 'required|integer|min:0',
'in_stock' => 'required|integer',
'on_sale' => 'required|boolean'
]);
$inventory = new Inventory();
$inventory->fill($validated)->save();
return redirect('/inventories');
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function edit(Inventory $inventory): \Illuminate\Contracts\View\View
{
return view('pages.inventories.edit',[
"inventory" => $inventory
]);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Inventory $inventory
* @return RedirectResponse
* @throws ValidationException
*/
public function update(Request $request, Inventory $inventory): RedirectResponse
{
$validated = $this->validate($request, [
'title'=> 'required|string',
'description'=> 'required|string|max:300',
'price' => 'required|integer|min:0',
'in_stock' => 'required|integer',
'on_sale' => 'required|boolean'
]);
$inventory->fill($validated)->save();
return redirect()->route('inventories.index')->with('status',
'Item has been updated!' . $inventory->title);
}
/**
* Remove the specified resource from storage.
*
* @param Inventory $inventory
* @return RedirectResponse
*/
public function destroy(Inventory $inventory): RedirectResponse
{
$inventory = Inventory::find($inventory);
$inventory->delete();
return redirect()->route('inventories.index')->with('status',
'Item has been deleted!' . $inventory->title);
}
}
这是我的 delete.blade 文件:
@extends('layouts.app')
@section('title', 'Delete Inventory')
@section('content')
<h1><strong>Delete inventory</strong></h1>
<x-inventory-form :inventory=$inventory />
{{ $inventory }}
<form method="POST" action="{{url('/inventories',[$inventory->id])}}"></form>
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@endsection
这是我的 inventory.blade 文件:
@extends('layouts.app')
@section('title', 'My Inventory')
@section('content')
<h1>Inventory Table</h1>
<p>This is the inventory table made using PHP Laravel that is randomly generated.</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Price</th>
<th>In stock</th>
<th>On sale</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($inventories as $inventory)
<tr>
<td>{{$inventory->id}}</td>
<td>{{$inventory->title}}</td>
<td>{{$inventory->description}}</td>
<td> £{{ number_format($inventory->price, 2) }}</td>
<td>{{$inventory->in_stock}}</td>
<td>{{ $inventory->on_sale ? 'Yes' : 'No' }}</td>
<td><a href="{{ route('inventories.edit', $inventory) }}">Edit</a></td>
<td><a href="{{ route('inventories.destroy', $inventory) }}">Delete</a></td>
</tr>
@endforeach
</tbody>
</table>
@endsection
这是我的库存表单组件:
<?php
namespace App\View\Components;
use App\Models\Inventory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class InventoryForm extends Component
{
/**
* @var Inventory|null
*/
public $inventory;
/**
* @param Inventory|null $inventory
*/
public function __construct(Inventory $inventory = null)
{
$this->inventory = $inventory;
}
/**
* @return string
*/
public function action(): string
{
if ($this->inventory) {
return route('inventories.update', ['inventory' => $this->inventory]);
}
return route('inventories.store');
}
/**
* Get the view / contents that represent the component.
*
*
* @return View|\Closure|string
*/
public function render()
{
return view('components.inventory-form');
}
这是我的库存表单组件刀片文件:
<form action="{{ $action }}" method="post">
@if($inventory)
@method('patch')
@endif
@csrf
<label for="title">Enter an item name:</label>
<input type="text" name="title" value="{{ $inventory?->title }}" required /><br><br>
<label for="description">Enter the item's description:</label>
<textarea name="description" required>{{ $inventory?->description }}</textarea><br><br>
<label for="price">Enter the item's price:</label>
<input type="number" name="price" value="{{ $inventory?->price }}" required/><br><br>
<label for="in_stock">Enter a number of items in stock:</label>
<input type="number" name="in_stock" value="{{ $inventory?->in_stock }}" required/><br><br>
<label for="on_sale">Select yes/no if item is on sale:</label>
<select name="on_sale" value="{{ (int) $inventory?->on_sale }}" required>
<option value="" disabled {{ $inventory ? '' : 'selected' }}>--Please choose an option--</option>
<option value="1" {{ $inventory?->on_sale ? 'selected' : '' }}>Yes</option>
<option value="0" {{ (false === $inventory?->on_sale) ? 'selected' : '' }}>No</option>
</select><br><br>
<button type="submit">Submit</button>
</form>
任何帮助将不胜感激!
【问题讨论】:
-
我猜,你的 delete.blade 文件是用表单包装的,对吧?
-
是的,我有一个inventoryfrom类和一个inventory-form刀片文件。