我正在尝试使用 ajax/jquery 制作动态表。
显示搜索结果后,我希望每一行都有像编辑和删除这样的操作来负责每个条目。这是我的控制器方法
public function search(Request $request){
if($request->ajax()){
$output="";
// $users=DB::table('users')->where('user_id','LIKE','%'.$request->search."%")->get();
$grammar_cards = FlashCard::where('lang','=','gr')
->where(function($query) use ($request){
$query->where('word','LIKE', '%'.$request->search.'%')
->orWhere('option1','LIKE', '%'.$request->search.'%')
->orWhere('option2','LIKE', '%'.$request->search.'%')
->orWhere('option3','LIKE', '%'.$request->search.'%')
->orWhere('option_correct','LIKE', '%'.$request->search.'%')
->orderBy('class')
->orderby('unit_id');
})->paginate(20);
// $flashcards->setPath('custom/url');
if($grammar_cards){
foreach ($grammar_cards as $key => $grammar_card) {
$grammarcard_question = $grammar_card->word;
$grammarcard_ch1 = $grammar_card->option1;
$grammarcard_ch2 = $grammar_card->option2;
$grammarcard_ch3 = $grammar_card->option3;
$grammarcard_correctchoice = $grammar_card->option_correct;
$grammarcard_unit = $grammar_card->unit_id;
$grammarcard_class = $grammar_card->class;
$output.='<tr>'.
'<td>'.$grammarcard_question.'</td>'.
'<td>'.$grammarcard_ch1.'</td>'.
'<td>'.$grammarcard_ch2.'</td>'.
'<td>'.$grammarcard_ch3.'</td>'.
'<td>'.$grammarcard_correctchoice.'</td>'.
'<td>'.$grammarcard_unit.'</td>'.
'<td>'.$grammarcard_class.'</td>'.
'<td style="display:flex;">
<a href="/admin/grammar/'.$grammar_card->id.'/edit" style="margin-right:10px;"><button class="btn btn-info">რედაქტირება</button></a>
<form action="{{ route(\'grammar.destroy\', '.$grammar_card->id.')}}" method="post">
@csrf
@method(\'DELETE\')
<button class="btn btn-danger" type="submit">delete</button>
</form></td>'.
'</tr>';
}
return Response($output);
// return view('admin.grammar.index', ['output' => $output])->render();
}
}
}
这是我的路线
Route::resource('/admin/grammar', 'GrammarController');
这是我的销毁方法
public function destroy($id)
{
$grammar_card = FlashCard::find($id);
$grammar_card->delete();
return redirect('admin/grammar')->with('success', 'successfully deleted');
}
它打印出@csrf @method('DELETE') 作为文本和删除按钮
当我单击删除按钮 时,它转到 url localhost:8000/admin/%7B%7B%20route('grammar.destroy',%2040271)%7D%7D 并输出 419 错误
你能告诉我如何解决这个问题吗?