ajax 的嵌套资源控制器和路由
Nested resource controllers and routes with ajax
我为 web.php
使用了以下代码。
Route::resource('products', 'ProductController');
Route::resource('products.galleries', 'GalleryController');
在这个地址 http://localhost:8000/admin/products ,我添加了这段代码
<td><a href="{{ route('products.galleries.create', $product->id) }}">Create</a></td>
然后我在create
方法里定义了,GalleryController
的控件
GalleryController.php
public function create(Product $product)
{
return view('Admin.galleries.create', compact('product'));
}
画廊是空的
我正在使用 下拉菜单
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
type: 'POST',
url: '{{ route('products.galleries.destroy', [$product->id, $gallery->id]) }}',
date: {filename, name},
success: function (data) {
console.log('File has been successfully removed!!');
},
error: function (e){
console.log(e);
},
});
我收到这个错误
Undefined variable: gallery
谁能告诉我如何解决这个问题?
问题很简单,因为 ajax 的 url
部分没有定义 $gallery
变量。
你能解释一下 destroy
路由中 $gallery
变量的用途是什么以及你如何将它传递给 create
blade.
在您的 GalleryController
中,图库实例 ($gallery
) 永远不会发送到视图:
public function create(Product $product)
{
return view('Admin.galleries.create', compact('product')); // where is $gallery ?
}
这就是为什么在您看来,当您尝试使用 $gallery
(在您的 javascript 销毁路线中)时,您会收到错误消息。
您有多种方法可以解决此问题,具体取决于您要实现的目标。但是,如果没有更多信息,最快的解决方案是将您的 javascript 代码包装在一个条件中。
@isset($gallery)
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
type: 'POST',
url: '{{ route('products.galleries.destroy', [$product->id, $gallery->id]) }}',
date: {filename, name},
success: function (data) {
console.log('File has been successfully removed!!');
},
error: function (e){
console.log(e);
},
});
@endisset
我猜你这样做是因为你想在 $gallery
变量可用时重用视图的这一部分,例如 products.galleries.index
或 products.galleries.show
.
通过将 javascript 包装在 @isset($gallery)
标记中,您将确保它仅在 $gallery
实例可用时执行。
我为 web.php
使用了以下代码。
Route::resource('products', 'ProductController');
Route::resource('products.galleries', 'GalleryController');
在这个地址 http://localhost:8000/admin/products ,我添加了这段代码
<td><a href="{{ route('products.galleries.create', $product->id) }}">Create</a></td>
然后我在create
方法里定义了,GalleryController
GalleryController.php
public function create(Product $product)
{
return view('Admin.galleries.create', compact('product'));
}
画廊是空的
我正在使用 下拉菜单
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
type: 'POST',
url: '{{ route('products.galleries.destroy', [$product->id, $gallery->id]) }}',
date: {filename, name},
success: function (data) {
console.log('File has been successfully removed!!');
},
error: function (e){
console.log(e);
},
});
我收到这个错误
Undefined variable: gallery
谁能告诉我如何解决这个问题?
问题很简单,因为 ajax 的 url
部分没有定义 $gallery
变量。
你能解释一下 destroy
路由中 $gallery
变量的用途是什么以及你如何将它传递给 create
blade.
在您的 GalleryController
中,图库实例 ($gallery
) 永远不会发送到视图:
public function create(Product $product)
{
return view('Admin.galleries.create', compact('product')); // where is $gallery ?
}
这就是为什么在您看来,当您尝试使用 $gallery
(在您的 javascript 销毁路线中)时,您会收到错误消息。
您有多种方法可以解决此问题,具体取决于您要实现的目标。但是,如果没有更多信息,最快的解决方案是将您的 javascript 代码包装在一个条件中。
@isset($gallery)
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
type: 'POST',
url: '{{ route('products.galleries.destroy', [$product->id, $gallery->id]) }}',
date: {filename, name},
success: function (data) {
console.log('File has been successfully removed!!');
},
error: function (e){
console.log(e);
},
});
@endisset
我猜你这样做是因为你想在 $gallery
变量可用时重用视图的这一部分,例如 products.galleries.index
或 products.galleries.show
.
通过将 javascript 包装在 @isset($gallery)
标记中,您将确保它仅在 $gallery
实例可用时执行。