我如何在laravel中获得职位ID?

igetnqfo  于 2022-11-26  发布在  其他
关注(0)|答案(3)|浏览(138)

我是新的Laravel,所以我试图删除和编辑一些帖子,这是链接到一个页面的更新,但每次我更新或删除,我得到一个404错误或页面没有找到(我认为问题是网址)。
下面是我的更新代码

public function update(Request $request, $id)   {
$car = Car::where('id', $id)
     ->update([
        'name'=> $request->input('name'),
        'founded'=> $request->input('founded'),
        'description' => $request->input('description')
]);

return redirect('/cars'); }

此选项用于删除/销毁

public function destroy($id)
{
    $car = Car::find($id);
    $car->delete();
    return redirect('/cars');
}

我也有一个edit.blade.php

@section('content')
<div class="m-auto w-4/8 py-24">
    <div class="text-center">
        <h1 class="text-5xl uppercase bold">
            Update Car
        </h1>
    </div>
</div>
<div class="flex justify-center pt-20">
    <form action="../cars/{{ $car->id }}" method="POST">
        @csrf
        @method('PUT')
        <div class="block">
            <input type="text" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="name" 
            value="{{ $car->name }}"><br>

            <input type="number" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="founded" 
            value="{{ $car->founded }}"><br>

            <input type="text" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="description" 
            value="{{ $car->description }}"><br>

            <button type="submit" class="bg-teal-500 block shadow-5xl mb-10 p-2 w-80 uppercase font-bold text-white">
              Update
            </button>
            
        </div>
    </form>
</div>

@结束部分
最后一部分包含删除和编辑按钮

@foreach ($cars as $car )
            <div class="m-auto">
                
                <span class="uppercase text-teal-500 font-bold text-xs italic">
                    Founded : {{ $car->founded  }}
                </span>
                <h2 class="text-gray-700 text-5xl">
                  {{ $car->name }}
                </h2>
                <p class="text-lg text-gray-700 py-6">
                    Description : {{ $car->description }} 
                 </p>
                 <div class="float-right">
                    <a class=" pb-2 italic text-teal-500" href="cars/{{ $car->id }}/edit">
                      Edit &rarr;
                    </a>

                    <form action="../cars/{{ $car->id }}" method="POST">
                     @csrf
                     @method("delete")
                    <button type="submit" class="pb-2  italic text-red-500">
                        Delete &rarr;
                    </button>
                    </form>
                  </div><br><br>

            <hr class="mt-4 mb-8">
            </div>
       @endforeach

这是我路线

Route::resource('/cars', CarsController::class);
ffdz8vbo

ffdz8vbo1#

首先使用命令php artisan route:list检查路由,然后您会看到如下列表

DELETE          cars/{car}
PUT|PATCH       cars/{car}

car名称对于Laravel基于类型提示Car $car自动查找实体非常重要,因此在控制器中使用以下约定:

public function destroy(Car $car)
{
    $car->delete();
    return redirect('/cars');
}
public function update(Request $request, Car $car) { ... }
o2rvlv0m

o2rvlv0m2#

您不应生成如下URL:action="../cars/{{ $car->id }}"
请改用action="{{ route('cars.update', $car->id) }}"
您可以通过运行以下命令php artisan route:list来查看可用路由

5jvtdoz2

5jvtdoz23#

所以,基本上当你使用资源,你得到预定义的路线列表由Laravel与不同的方法.
您路线示例为

Route::resource('/cars', CarsController::class);

然后laravel生成如下路线要检查路线列表,请运行php artisan route:list

Route::GET('/cars', [CarsController::class, 'index'])->name('cars.index');
Route::GET('/cars/create', [CarsController::class, 'create'])->name('cars.create');
Route::POST('/cars', [CarsController::class, 'store'])->name('cars.store');
Route::GET('/cars/{id}', [CarsController::class, 'show'])->name('cars.show');
Route::GET('/cars/{id}/edit', [CarsController::class, 'edit'])->name('cars.edit');
Route::PUT('/cars/{id}', [CarsController::class, 'update'])->name('cars.update');
Route::DELETE('/cars/{id}', [CarsController::class, 'destroy'])->name('cars.destroy');

然后您可以在表单中使用已定义的方法。
第一次
来源Laravel文件:点击查看Laravel官方网站上更多资源方法。

相关问题