簡體   English   中英

在Laravel中以多對多關系顯示要刪除的數據的問題

[英]Problem with displaying data that I want to delete with many to many relationship in Laravel

我與用戶和產品表之間有很多關系,我建立了一個視圖(welcome.blade.php),當我單擊產品名稱(這是一個鏈接)時,應該將我重定向到刪除表格的頁面是這樣,我可以刪除該特定產品,但出現“ 404 not found”頁面。 我懷疑錯誤可能出在我的路線上,但我似乎找不到問題。 另外,當我單擊某些產品時,我的網址顯示為project / destroy / 1,我認為這很好。 這是我的代碼:

web.php:

Route::get('/home', 'HomeController@index')->name('home');
Route::post('/store', 'HomeController@store')->name('store');
Route::delete('/destroy/{$id}', 'HomeController@destroy')->name('destroy');

destroy.blade.php:

<div class="col-md-12">
    <form action="destroy/{{ $product->id }}" method="POST">
     @csrf
     @method('DELETE')
        <button type="submit" class="btn btn-danger">Delete</button>
    </form>
</div>

welcome.blade.php:

@if($products)
<table class="table">
    <thead>
        <th>#</th>
        <th>Product Name</th>
        <th>Owner Of The Product</th>
        <th>Created At</th>
    </thead>
    <tbody>
    @foreach ($products as $product)
        <tr>
            <td>{{ $product->id }}</td>
            <td>
                <a href="{{ route('destroy', $product->id) }}">{{ $product->files }}</a>
            </td>
            <td>
                @foreach ($product->users as $user) {{ $user->name }}
                @endforeach
            </td>
            <td>{{ date('M j, Y', strtotime($product->created_at)) }}</td>
        </tr>
     @endforeach
     </tbody>
     </table>
 @endif

HomeController.php:

<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    public function destroy(Product $product)
    {
        $product->users()->detach();
        $product->delete();
        return view('destroy');
    }
}

您在文件中定義了以下路由。

Route::delete('/destroy/{$id}', 'HomeController@destroy')->name('destroy');

這將創建一個DELETE路線。 刪除路由應由API訪問。 定位標記中的鏈接使用GET路由。 點擊

<a href="{{ route('destroy' , $product->id) }}">{{ $product->files }}</a>

使路由器嘗試匹配未定義的GET /destroy/{$id} ,因此如果打開了調試,它將拋出異常,否則,將拋出404。 您可以通過查看瀏覽器開發者控制台的“網絡”標簽來查看實際情況。

除非您添加其他GET路線,否則您將不斷獲得404。

Route::get('/destroy/{$id}', 'HomeController@destroy')->name('product.destroy-form');

將使以下鏈接起作用。

<a href="{{ route('product.destroy-form' , $product->id) }}">{{ $product->files }}</a>

另外,在destroy方法中,您將返回視圖而未傳遞任何變量,但在destroy.blade.php您似乎正在使用$product 不要忘記添加它!

我認為這是因為要使用新頁面刪除產品,首先要做的是導航到您未使用的頁面。 您而是直接使用此功能進入刪除功能

Route::delete('/destroy/{$id}', 'HomeController@destroy')->name('destroy');

您需要定義一條路線,讓您首先進入該頁面,您可以通過創建如下路線來做到這一點:

Route::get('/delete/{$id}', 'HomeController@delete')->name('delete');

您應該在HomeController中創建一個新的刪除函數,該函數僅返回destroy.blade.php。 這樣的事情。

$product= Product::find($id);
    return view('destroy')->with('product', $product);

其中$ id是產品ID,而Product是您使用的型號。

您快要到那里了,步伐太多了。

良好地調用表單並使用delete方法,這絕對是您應該做的

但是,您出問題的地方是使用鏈接轉到帶有要刪除的表單的單獨頁面。 您最好使用一些JavaScript,以便該鏈接從該鏈接提交隱藏的表單。

<a onclick="document.getElementById('delete-form-{{$product->id}}').submit();}">{{ $product->files }}</a>
<form id="delete-form-{{$product->id}}" action="{{ route('destroy', ['id' => $product->id]) }}" method="POST" style="display: none;">
    @csrf
    @method('delete')
</form>

您可以執行已有的方法,但是需要附加的route :: get請求到加載表單的頁面,然后可以在該頁面上提交表單以刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM