2017-10-27 2 views
0

I Verfahren folgende Ressourcensteuerung mit zerstören:Laravel 5.5-Controller @ DI destroy nicht Modell injizieren

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

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

Auch habe ich Gitter mit folgenden Zeilen:

<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> 

Und schließlich habe ich Funktion Form auf die Schaltfläche klicken senden:

$('.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; 
} 

Wenn ich das senden Formular /admin/clinic/1/clinic-image/1, ich fo llowing error: Type error: Argument 1 passed to App\Http\Controllers\Admin\ClinicImageController::destroy() must be an instance of App\ClinicImage, string given

Steuerungsrouten. my routes

Also meine Frage ist: Warum DI meine Route und Modell-ID nicht erkennen?

+1

Sie senden zwei Parameter und expection nur ein, versuchen Sie dies: public function zerstören ($ Klinik, ClinicImage $ klinikImage) – Roots

Antwort

2

Versuchen Sie diese:

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', 'Изображение удалено'); 
} 
+0

Ihre Antwort teilweise richtig, ich habe gerade die Abhängigkeit für 'Klinik $ Klinik 'hinzugefügt –

0

Eigentlich sind Sie in der Steuerung falschen Argument-Namen.

Ändern Sie Ihre Unterschrift in einem Controller.

Statt $clinicImage Änderung es $clinic_image

Option 1: Veränderung der Controller

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

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

Option 2: Parameter ändern in Routendatei:

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

Ich kann Route Parameter nicht ändern, weil Controller Ressource ist. –

0

Versuchen Sie dies in Ihrem Controller-Datei

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

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

Und in Ihrer Ansicht Datei

<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>