首页 > 解决方案 > 如何将数据和 url 从 ajax 传递到控制器?

问题描述

我有一个产品页面,我的画廊是产品的一个子类别。

Route::resource('products.galleries', 'GalleryController');

如何将两个 ID 传输到控制器?

阿贾克斯

@isset($gallery)
$.ajax({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    },
    type: 'POST',
    url: '{{ route('products.galleries.destroy', [$product->id, $gajllery->id]) }}',
    data : {
        "_method" : "DELETE" ,
        name
    },
    success: function (data) {
        console.log(data);
    },
    error: function (e){
        console.log(e);
    },
});
@endisset

GalleryController.php

public function destroy(Product $product, Request $request)
{
    $filename = $request->get('name');
    dd($filename);
    Gallery::where('image', $filename)->delete();
    Storage::disk('public')->delete($filename);
}

它甚至没有向我们展示dd($filename);

注意:此代码完全没有错误。

标签: phpajaxlaravel

解决方案


推荐阅读