繁体   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