Dlatego drukuję skargi użytkowników w tabeli, w której w każdym wierszu drukuję również przycisk Usuń. Kiedy klikam ten przycisk usuwania, chcę usunąć tę konkretną skargę z tabeli. Nie używam do tego kontrolera zasobów, ale kontrolera podstawowego. A teraz mój kod:
ViewComplaint.blade.php (Tabela skarg z przyciskiem Usuń):
<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
Web.php (Trasy):
Route::get('/complaint/create','ComplaintController@create')->name('complaint.create');
Route::post('/complaint','ComplaintController@store')->name('complaint.store');
Route::get('/complaint','ComplaintController@index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController@destroy')->name('complaint.destroy');
ComplaintController.php (kontroler podstawowy):
class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user = User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}
Teraz, kiedy klikam przycisk Usuń na stole, pojawia się po prostu błąd „404 | Nie znaleziono”. Co ja tu robię źle? Naprawdę byłbym wdzięczny za pomoc.
3 odpowiedzi
Usuń $ id z trasy
Route::delete('/complain/{id}','ComplaintController@destroy')->name('complaint.destroy');
public function destroy($id) {
}
Parametr trasy to tylko nazwa; mówisz, że ten konkretny segment trasy jest dynamiczny i chcę mieć parametr o nazwie complaint
:
Route::delete('complaint/{complaint}', 'ComplaintController@destroy')->name('complaint.destroy');
Następnie możesz dostosować metodę destroy
tak, aby przyjmowała parametr complaint
ze wskazówkami o typie jako Complaint $complaint
, aby uzyskać niejawne powiązanie:
public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}
{!! Form::open(array('route'=>['complaint.destroy',$complaint],'method'=>'DELETE')) !!} {!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm']) !!} {!! Form::close() !!}
, a to jest mój kontroler: public function destroy(Complaint $complaint) { $complaint->delete(); return redirect()->route('complaint.index'); }
Wydaje mi się, że źle określasz trasę. Zmień trasę na:
Route::delete('/complaint/{id}','ComplaintController@destroy')->name('complaint.destroy');
Nie potrzebujesz tablicy () w otwieraniu formularza, więc zmień otwarcie formularza na to:
{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}
I usuń $complaint->id
z przycisku przesyłania, nie potrzebujesz go tam.
Wszystko, co musisz teraz zrobić w swojej funkcji, to znaleźć Complaint
, który ma id
przekazany w formularzu:
public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}
Daj mi znać, jeśli napotkasz jakieś błędy.
array(1, 2, 3)
to to samo co [1, 2, 3]
]
na tablicy w twoim przykładzie ;)
Podobne pytania
Nowe pytania
php
PHP to szeroko stosowany, wysokopoziomowy, dynamiczny, zorientowany obiektowo i interpretowany język skryptowy przeznaczony głównie do tworzenia stron WWW po stronie serwera. Używane w przypadku pytań dotyczących języka PHP.