Laravel 2以相同的形式提交按钮

时间:2019-05-25 08:27:07

标签: laravel

我正在与Laravel建立CRUD。每个类别都有许多附件,每个附件都属于一个类别。

在category.edit视图中,我想让用户可以从类别中删除附件(单个)。我尝试了这种方法,但是没有用:

注册附件的路由:

tapply(Mutalau$yield,Mutalau$type,shapiro.test)
$`1`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.86025, p-value = 0.19


$`2`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.92557, p-value = 0.5463


$`3`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.69923, p-value = 0.006067


$`4`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.76799, p-value = 0.02971


$`5`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.87673, p-value = 0.2544


$`6`

    Shapiro-Wilk normality test

data:  X[[i]]
W = 0.78526, p-value = 0.04313

处理构建AttachmentController @ delete方法的删除:

Route::group(['middleware' => ['auth']], function () {
    Route::delete('attachment/{id}', 'AttachmentController@delete')->name('attachment');    
});

在CategoryController(编辑方法)中,我获取链接到要在视图内部呈现的每个类别的附件:

class AttachmentController extends Controller
{
    public function delete($id) {

        $toDelete = Attachment::findOrFail($id);
        $toDelete->delete();
        return redirect()->back();
    }
}

在视图中,我呈现附件和要删除的按钮。我完全知道在另一个表单中包含一个表单的错误,但是我不知道提交此删除请求的其他方法。

public function edit($category)
{
    $wildcard = $category;
    $category = Category::findOrFail($wildcard);
    $attachments = App\Category::findOrFail($wildcard)->attachments()->get()->toArray();
    return view('category.edit', [
        'category' => $category,
        'attachments' => $attachments
    ]);
}

我应该在CategoryController内构建一个deleteAttachment方法吗?如果是这样,我如何仍可以提交删除请求?另外,如果将来有其他任何Model带有附件,是否应该在每个控制器中构建deleteAttachment方法?那很麻烦。预先感谢

1 个答案:

答案 0 :(得分:0)

如果您不喜欢使用表单,请使用标签:

<a class="btn btn-danger" href="{{ route('attachment', $attachment['id']) }}">Delete Image</a>

并重新定义到Route::get(...)

的路线

(如果需要,也可以使用ajax作为POST方法)