Laravel删除不适用于Ajax

时间:2018-08-08 17:10:48

标签: laravel

public function show(Request $request)
    {
        
        $title = $request->title;
        $currencies = Currency::where('title', 'LIKE', "%{$title}%")->get();
        if (count($currencies) ){
            $data = "";
            $data .= " <table class='table table-striped' id='table'>  
                        <thead>
                        <tr><th scope='col'>ID</th><th scope='col'>Currency</th><th scope='col'>Country</th><th scope='col' colspan='2' style='text-align:right'>Actions</th></tr>
                        </thead>"; 
            foreach($currencies as $currency){
            $data .= "<tr><td scope='col'>{$currency->id}</td><td scope='col'>{$currency->title}</td><td scope='col'>{$currency->country}</td>";
            $data .="<td>
            {!!Form::open(['action'=>['CurrencyController@destroy', {$currency->id}], 'method'=>'POST'])!!}
            {!! Form::hidden('_method', 'DELETE') !!}
            @csrf
                <button type='submit' class='btn btn-danger btn-sm' style='float:right'><i class='fa fa-remove' style='color:white;'></i>Delete</button>   
                <a href='/currency/{$currency->id}/edit' class='btn btn-primary btn-sm' style='float:right' ><i class='fa fa-edit' style='color:white;'></i>Edit</a>       
            {!!Form::close() !!} 
            </td></tr>";
        }
            $data .='</table>'; 

        }else {
            $data = "No Available";
        }
        return response()->json($data);
    }

嗨,我正在使用Ajax搜索记录,并且在控制器内部生成了html代码,如下所示。但是如果我这样写的话,laravel Collective无法正常工作。而是只打印类似标签的显示。有人可以帮我解决这个问题吗?

我尝试添加如下表格

$data .="<td>
            <form action='/currency/{$currency->id}' method='POST'>         
            <input type='hidden' name='_method' value='DELETE'>     
            <input type='hidden' name='_token' value = '<?php echo csrf_token() ?>'>   
                <button type='submit' class='btn btn-danger btn-sm' style='float:right'><i class='fa fa-remove' style='color:white;'></i>Delete</button>   
                <a href='/currency/{$currency->id}/edit' class='btn btn-primary btn-sm' style='float:right' ><i class='fa fa-edit' style='color:white;'></i>Edit</a>       
            </form>
            </td></tr>";

但是我收到“该页面由于不活动而过期。请刷新并重试。”错误。

0 个答案:

没有答案