在 Laravel 中显示我想删除的具有多对多关系的数据时出现问题
Problem with displaying data that I want to delete with many to many relationship in Laravel
我在用户和产品之间建立了多对多关系 table,我设置了一个视图 (welcome.blade.php),当我点击产品名称时,它是一个 link ,它应该将我重定向到我的删除表单所在的页面,以便我可以删除该特定产品,但我得到 404 not found 页面。我怀疑错误出在我的路线中,但我似乎找不到问题所在。此外,当我单击某些产品时,我的 url 显示 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 路由。 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');
将使以下 link 工作。
<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');
您应该在 returns destroy.blade.php 的 HomeController 中创建一个新函数 delete。像这样的东西。
$product= Product::find($id);
return view('destroy')->with('product', $product);
其中 $id 是产品 ID,Product 是您使用的型号。
你快到了,只是差了一步。
很好地调用了表单并使用了删除方法,这绝对是你应该做的
然而,您出错的地方在于使用 link 转到带有要删除的表单的单独页面。您最好使用一点 javascript,以便 link 从 link.
提交隐藏表单
<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 请求,然后您可以提交该页面上的表单以将其删除。
我在用户和产品之间建立了多对多关系 table,我设置了一个视图 (welcome.blade.php),当我点击产品名称时,它是一个 link ,它应该将我重定向到我的删除表单所在的页面,以便我可以删除该特定产品,但我得到 404 not found 页面。我怀疑错误出在我的路线中,但我似乎找不到问题所在。此外,当我单击某些产品时,我的 url 显示 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 路由。 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');
将使以下 link 工作。
<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');
您应该在 returns destroy.blade.php 的 HomeController 中创建一个新函数 delete。像这样的东西。
$product= Product::find($id);
return view('destroy')->with('product', $product);
其中 $id 是产品 ID,Product 是您使用的型号。
你快到了,只是差了一步。
很好地调用了表单并使用了删除方法,这绝对是你应该做的
然而,您出错的地方在于使用 link 转到带有要删除的表单的单独页面。您最好使用一点 javascript,以便 link 从 link.
提交隐藏表单<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 请求,然后您可以提交该页面上的表单以将其删除。