简体   繁体   English

我如何在 laravel 中获取帖子 ID

[英]How do i get a post id in laravel

i am new to Laravel so am trying to delete and edit some posts which is linked to a page where the update from is located but each time i update or delete, i get a 404 error or the page is not found(i think the problem is the url).我是 Laravel 的新手,所以我试图删除和编辑一些链接到更新所在页面的帖子,但每次更新或删除时,我都会收到 404 错误或找不到页面(我认为问题是网址)。

here is my code for the update这是我的更新代码

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'); }

this one is for delete/destroy这是用于删除/销毁的

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

i also have an edit.blade.php我还有一个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>

@endsection @endsection

the last part contains the buttons for delete and edit最后一部分包含删除和编辑按钮

 @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

here is my route这是我的路线

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

first check route with this command php artisan route:list then you see list like this首先使用此命令检查路线php artisan route:list然后你会看到这样的列表

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

the car name is important to automatically Laravel find entity base on Type hint Car $car , so in controller use this convention: car名称对于自动 Laravel 根据类型提示Car $car查找实体很重要,因此在 controller 中使用以下约定:

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

You should not generate url like this: action="../cars/{{ $car->id }}"您不应该像这样生成 url: action="../cars/{{ $car->id }}"

Instead use action="{{ route('cars.update', $car->id) }}"而是使用action="{{ route('cars.update', $car->id) }}"

You can see the available routes by running this command php artisan route:list您可以通过运行此命令php artisan route:list查看可用路由

So, Basically when you use resource you get predefined route list by Laravel with different methods.因此,基本上,当您使用资源时,您会通过 Laravel 使用不同的方法获得预定义的路线列表。

Example your route is例如你的路线是

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

Then laravel generate routes like this.然后 laravel 生成这样的路由。 To check route list run php artisan route:list要检查路线列表,请运行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');

Then you can use in form with defined methods.然后您可以在表单中使用已定义的方法。

 Example to use
 {{ route('cars.destroy', ['id' => $car->id]) }}

Source Laravel documentation: click to check more resource method on offical Laravel website.来源 Laravel 文档: 单击以查看官方 Laravel 网站上的更多资源方法

You need to specify a route in routes.php您需要在 routes.php 中指定路由

Route::post('/cars/{id}', ......); Route::post('/cars/{id}', ......);

Then in the controller and function you are calling with that route the {id} will be a parameter然后在 controller 和 function 中,您使用该路由调用 {id} 将是一个参数

public function myControllerRoute($id, Request $request) {

}

You can also instead pass the ID as a hidden input field and retrieve it on /cars route with the $request您也可以将 ID 作为隐藏输入字段传递,并使用 $request 在 /cars 路由上检索它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM