简体   繁体   English

在Laravel中以多对多关系显示要删除的数据的问题

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

I have many to many relationship between users and products table and I set up a view (welcome.blade.php) that when I click on the product name, which is a link, it is supposed to redirect me to page where my delete form is so I can delete that particular product, but I get 404 not found page. 我与用户和产品表之间有很多关系,我建立了一个视图(welcome.blade.php),当我单击产品名称(这是一个链接)时,应该将我重定向到删除表格的页面是这样,我可以删除该特定产品,但出现“ 404 not found”页面。 I suspect that error is somewhere in my routes but I can't seem to find the problem. 我怀疑错误可能出在我的路线上,但我似乎找不到问题。 Also when I click on some product my url says project/destroy/1 which I think is good. 另外,当我单击某些产品时,我的网址显示为project / destroy / 1,我认为这很好。 Here is my code: 这是我的代码:

web.php: 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: 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: 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: 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');
    }
}

You defined the following route in your file. 您在文件中定义了以下路由。

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

This creates a DELETE route. 这将创建一个DELETE路线。 DELETE routes are meant to be accessed by APIs. 删除路由应由API访问。 Links in anchor tags use GET routes. 定位标记中的链接使用GET路由。 Clicking on 点击

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

makes the router try and match GET /destroy/{$id} which is not defined, so it throws either an exception if debug is on or a 404 if not. 使路由器尝试匹配未定义的GET /destroy/{$id} ,因此如果打开了调试,它将抛出异常,否则,将抛出404。 You can see this in action by looking at the network tab of your browser's developer console. 您可以通过查看浏览器开发者控制台的“网络”标签来查看实际情况。

Unless you add another GET route, you'll keep getting 404s. 除非您添加其他GET路线,否则您将不断获得404。

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

will make the following link work. 将使以下链接起作用。

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

Also, in the destroy method you're returning the view without passing any variables but in destroy.blade.php you seem to be using $product . 另外,在destroy方法中,您将返回视图而未传递任何变量,但在destroy.blade.php您似乎正在使用$product Don't forget to add it! 不要忘记添加它!

I think it is because to delete a product using a new page the first thing to do is navigate to that page which you are not doing. 我认为这是因为要使用新页面删除产品,首先要做的是导航到您未使用的页面。 You are instead going directly to the delete function using this 您而是直接使用此功能进入删除功能

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

You need to define a route that lets you go to that page first which u can do by creating a route like this: 您需要定义一条路线,让您首先进入该页面,您可以通过创建如下路线来做到这一点:

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

You should create a new function delete in the HomeController that just returns the destroy.blade.php. 您应该在HomeController中创建一个新的删除函数,该函数仅返回destroy.blade.php。 Something like this. 这样的事情。

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

where $id is the product id and Product is the model that you are using. 其中$ id是产品ID,而Product是您使用的型号。

You are nearly there, you just have a step too many. 您快要到那里了,步伐太多了。

Good call on the form and using the delete method, this is absolutely what you are supposed to do 良好地调用表单并使用delete方法,这绝对是您应该做的

However where you are going wrong is in using the link to go to a separate page with the form on to delete. 但是,您出问题的地方是使用链接转到带有要删除的表单的单独页面。 You are better using a little javascript so the link submits a hidden form from the link. 您最好使用一些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>

You can do the method you have but you need an additional route::get request to a page that loads the form and then you can submit the form on that page to delete. 您可以执行已有的方法,但是需要附加的route :: get请求到加载表单的页面,然后可以在该页面上提交表单以删除。

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

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