我在laravel中使用了此代码,并使用YAJRA作为我的数据表,并在提交时表示CSRF令牌是错误的,附在此处是我在Controller中的代码,然后在View / Blade中进行渲染。 这是我的代码:
$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;
答案 0 :(得分:0)
我通过文档https://laravel.com/docs/master/csrf找到了答案 我只是把所述表格的URI
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
'procurement/*',
];
}
答案 1 :(得分:0)
将表单返回到另一个刀片,例如view / products / datatables.blade.php
示例:控制器应类似于:-
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>
它将正常工作。因为在控制器中无法读取小胡子{{}}。我们将东西重定向到刀片上