dlf*_*fjj 2 datatable laravel laravel-datatables
在我的 Laravel 5.6 应用程序中,我尝试将一个$id变量从我的路由传递到我的数据表的每一列。
我的代码:
public function getVendorslistChange($id){
try {
return Datatables::of($this->purchaseRepository->getVendorListData(),$id)
->addColumn('action', function ($vendor,$id) {
return \Form::open(['method' => 'POST', 'action' => ['PurchaseController@postChangeVendor',$id], 'class' => 'form']) . '
<input type="hidden" name="id" value="' . $vendor->id . '" />
<input type="submit" name="submit" value="Choose" class="btn center-block" />
' . \Form::close();
})
->make(true);
} catch (\Exception $e) {
return $this->redirectWithErrors($e);
}
}
Run Code Online (Sandbox Code Playgroud)
这部分$this->purchaseRepository->getVendorListData()将返回以下内容:
public function getVendorListData(){
$this->vendors = Vendor::Select(
array(
'vendors.id',
'vendors.company_name'
))
->where('vendors.company_id',return_company_id())
->where('status','Active')->get()
;
return $this->vendors;
}
Run Code Online (Sandbox Code Playgroud)
但是有错误说,$id不能传递给addColumn.
函数 App\Http\Controllers\PurchaseController::App\Http\Controllers{closure}() 的参数太少,1 传入 /Applications/XAMPP/xamppfiles/htdocs/laravel-project/american_dunnage/vendor/yajra/laravel-datatables -oracle/src/Utilities/Helper.php 在第 64 行,正好是 2 行
将这样的参数传递给数据表的每一列的正确方法是什么?
如果供应商函数不支持它们,您不应该只是向它们添加参数。例如,当您调用 时Datatables::of(),源代码显示它只需要一个参数。因此,即使您传递了额外的$id变量,它$id也不会传递给您提供给addColumn(). 这就是为什么您会看到有关传入参数太少的错误的原因。
https://github.com/yajra/laravel-datatables/blob/8.0/src/DataTables.php#L33
像这样的事情可能会奏效。请注意我是如何将回调告诉 的use,$id而不是尝试将其直接传递到函数调用中:
public function getVendorslistChange($id){
try {
return Datatables::of($this->purchaseRepository->getVendorListData())
->addColumn('action', function ($vendor) use ($id) {
return \Form::open(['method' => 'POST', 'action' => ['PurchaseController@postChangeVendor',$id], 'class' => 'form']) . '
<input type="hidden" name="id" value="' . $vendor->id . '" />
<input type="submit" name="submit" value="Choose" class="btn center-block" />
' . \Form::close();
})
->make(true);
} catch (\Exception $e) {
return $this->redirectWithErrors($e);
}
}
Run Code Online (Sandbox Code Playgroud)
查看文档中的示例 3,了解如何管理匿名函数作用域:
http://php.net/manual/en/functions.anonymous.php
| 归档时间: |
|
| 查看次数: |
4147 次 |
| 最近记录: |