【发布时间】:2021-12-29 15:46:41
【问题描述】:
我遇到了一个问题,我想编辑我的库存列表中的一个项目,并且能够单击编辑按钮,但浏览器中没有任何反应。我很困惑为什么会这样。 任何想法都会非常有帮助。 我的控制器:
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
/**
* Class InventoryController
*/
class InventoryController extends Controller
{
/**
* Get the list of inventories.
*
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
$inventories = Inventory::all();
return view('pages.inventories',[
"inventories" => $inventories
]);
}
/**
* Show the generated inventory
*
* @return \Illuminate\Contracts\View\View
*/
public function create()
{
return view('pages.inventories.create');
}
/**
* Store a newly created inventory
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$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');
}
/**
* Editing existing inventory
*
* @param $inventory
*
*/
public function edit(Request $request, Inventory $inventory)
{
$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 = Inventory::find('id');
$inventory->title = $request->input('title');
$inventory->description = $request->input('description');
$inventory->price = $request->input('price');
$inventory->in_stock = $request->input('in_stock');
$inventory->on_sale = $request->input('on_sale');
$inventory->fill($validated)->save();
return view('pages.inventories.edit',['inventory' => $inventory])->with('Item has been updated!' . $request->input('title'));
}
我的库存刀片文件:
@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
use Illuminate\Support\Facades\Route;
Route::get('/', [\App\Http\Controllers\HomeController::class, 'index'])->name('pages.index');
Route::get('/inventories', [\App\Http\Controllers\InventoryController::class, 'index'])->name('index');
Route::get('/inventories/create', [\App\Http\Controllers\InventoryController::class, 'create']);
Route::post('/inventories', [\App\Http\Controllers\InventoryController::class, 'store']);
Route::get('/inventory/{inventory}/edit',[\App\Http\Controllers\InventoryController::class, 'edit'])->name('inventories.edit');
Route::patch('/inventories/{inventory}',[\App\Http\Controllers\InventoryController::class, 'edit'])->name('inventories.update');
Route::get('/inventories/{inventory}',[\App\Http\Controllers\InventoryController::class, 'delete'])->name('inventories.destroy');
Route::delete('/inventories/{inventory}', [\App\Http\Controllers\InventoryController::class, 'delete'])->name('inventories.destroy');
我的库存模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
use HasFactory;
/**
* @var array
*/
protected $casts = [
'on_sale' => 'boolean',
];
/**
* @var string[]
*/
protected $fillable = [
'title',
'description',
'price',
'in_stock',
'on_sale'
];
/**
* @var array
*/
protected $guarded = [];
/**
* @var string
*/
protected $primaryKey = 'id';
}
我的编辑刀片文件:
@extends('layouts.app')
@section('title', 'Create Inventory')
@section('menu')
@section('content')
<h1><strong>Edit {{ $inventory->title }}</strong></h1>
<x-inventory-form :inventory=$inventory />
@endsection
目标是让用户希望从我的数据库中的库存列表中编辑一个项目,并能够更改标题、描述、价格,无论是在库存还是在售,然后验证它,如果它是已验证使用成功消息更新项目,以便用户知道他们成功了。
【问题讨论】:
标签: php laravel model routes controller