YAJRA数据表Laravel5.5上的CSRF无法正常工作 [英] CSRF on YAJRA datatable Laravel5.5 not working
本文介绍了YAJRA数据表Laravel5.5上的CSRF无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将这段代码存储在变量中,因此我将其显示在视图中, 我尝试了打包"Form Header"和使用CSRF的另一种方法
I have this code where I needed to store in a variable so I display it in view, I've tried different approach of packing the "Form header" and using CSRF is not working
$return = '<form method="post" action="/procurement/add-product">
'.{{ csrf_token() }}.'
<input type="hidden" name= "product_id" value=".$row->id.">
<input type="text" name="product_qty" class="form-control">
<button type="submit" class="btn btn-primary btn-block">Add Item</button>
</form>';
return $return;
这是我的路线
Route::post('/procurement/add-product','ProductController@addProcurementProduct');
这是Java脚本部分
<script type="text/javascript">
$(function() {
$('#procurement-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url('procurement/get_procurement_datatable') }}',
columns : [
{data: 'id', name: 'id'},
{data: 'po_number', name: 'po_number'},
{data: 'action', searchable: false, orderable: false},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'}
]
});
});
</script>
推荐答案
将表单返回到另一个刀片,例如view/products/datatables.blade.php
Return the form to another blade like view/products/datatables.blade.php
示例:控制器应类似于:-
Example: The controller should looks like:-
public function getproducts()
{
$product = Product::all(); //Product is Model name
return Datatables::of($product)
->addColumn('action', function($product)
{
return view('product.datatables', compact('product'))->render();
})->make(true);
}
视图应如下所示:
<a href="{{ route('product.edit', ['$id' => $product->id]) }}" class="btn btn-success btn-sm">Edit</a>
<form action="/product/{{ $product->id }}" method="post" id="deleteForm">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger btn-sm" type="submit">Delete</button>
</form>
它将正常工作.因为在控制器中无法读取小胡子{{}}.我们将东西重定向到刀片上
It will work just fine. Because the mustache {{}} can't be read in the controller. We redirect the things to the blade
这篇关于YAJRA数据表Laravel5.5上的CSRF无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文