【问题标题】:Laravel 5.5 Controller@destroy DI won't inject modelLaravel 5.5 Controller@destroy DI 不会注入模型
【发布时间】:2017-10-28 21:34:18
【问题描述】:

我有以下销毁方法的资源控制器:

public function destroy(ClinicImage $clinicImage)
{
    $clinicImage->delete();

    return redirect()->back()->with('success', 'Изображение удалено');
}

我也有以下几行的网格:

<td>
    <div class="btn-group btn-group">
        <button type="button" data-url="{{route('admin.clinic-image.destroy', [$clinic->id, $image->id])}}" class="btn btn-danger">
            <i class="fa fa-remove fa-fw"></i>
        </button>
    </div>
</td>

最后我可以在按钮点击时发送表单:

$('.table').find('.btn.btn-danger').click(function(){
     var form = makeForm({_method: 'DELETE'},{action: $(this).data('url')});
     form.submit();
});

function makeForm(data, options) {
    var form = document.createElement('form');
    form.method = 'POST';

    var token = document.createElement('input');

    token.name = '_token';
    token.value = jQuery('meta[name="csrf-token"]').attr('content');

    form.appendChild(token);

    jQuery.each(data, function(key, value){
        var input = document.createElement('input');

        input.name = key;
        input.value = value;

        form.appendChild(input);
    });

    if(Object.keys(options).length) {
        jQuery.each(options, function(option, value){
            form[option] = value;
        });
    }

    document.body.appendChild(form);

    return form;
}

当我向/admin/clinic/1/clinic-image/1 发送表单时,我收到以下错误: Type error: Argument 1 passed to App\Http\Controllers\Admin\ClinicImageController::destroy() must be an instance of App\ClinicImage, string given

控制器路由。

所以我的问题是:为什么 DI 无法识别我的路线和模型 ID?

【问题讨论】:

  • 你发送了两个参数,只期望一个,试试这个:public function destroy($clinic, ClinicImage $clinicImage)

标签: laravel dependency-injection laravel-5.5


【解决方案1】:

试试这个:

public function destroy($clinic, $clinicImage)
{
    $clinicImage = ClinicImage::where('id', $clinic)->where('image', $clinicImage); //I'm guessing the name of the columns.
    $clinicImage->delete();

    return redirect()->back()->with('success', 'Изображение удалено');
}

【讨论】:

  • 你的回答部分正确,我刚刚添加了对Clinic $clinic的依赖
【解决方案2】:

实际上,您在控制器中使用了错误的参数名称。

在控制器中更改您的签名。

$clinicImage改为$clinic_image

选项 1:更改控制器

public function destroy(ClinicImage $clinic_image) //$clinicImage to $clinic_image
{
    $clinicImage->delete();

    return redirect()->back()->with('success', 'Изображение удалено');
}

选项 2: 更改路由文件中的参数:

Route::delete('/admin/clinic/{clinic}/clinic-image/{clinicImage})->name('');

【讨论】:

  • 我无法更改路由参数,因为控制器是资源。
【解决方案3】:

在你的控制器文件中试试这个

public function destroy(ClinicImage $clinicImage)
{
    $clinicImage->delete();

    return redirect()->back()->with('success', 'Изображение удалено');
}

在你的视图文件中

<td>
    <div class="btn-group btn-group">
        <button type="button" data-url="{{route('admin.clinic-image.destroy', [$clinic->id])}}" class="btn btn-danger"> (over here you need to pass 1 id at a time which you need to delete)
            <i class="fa fa-remove fa-fw"></i>
        </button>
    </div>
</td>

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 2018-02-09
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多