尝试编辑数据后出现此错误。 CRUDRenderHelper.php第41行中的FatalErrorException:调用字符串上的成员函数fill()
我的CarCategoriesController代码:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Models\CarCategory;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use App\Http\Controllers\Base\MasterController;
use Route;
use Illuminate\View\View;
class CarCategoriesController extends MasterController
{
protected $indexView = 'CarCategories.index';
protected $createView = 'CarCategories.create';
protected $editView = 'CarCategories.create';
protected $routeBindModel = 'CarCategories'; //Route Model Binding name in RouteServiceProvider
protected $redirectPageWhenFormSuccess = 'CarCategories.index'; //Route Name
protected $title = 'Car Categories';
public function save(Request $request, $obj = null) {
if (!$obj) {
$obj = new CarCategory;
}
return $this->saveHandler($request, $obj);
}
//Must have this method if need datatable;
public function datatableBuilder($obj = null) {
return CarCategory::query();
}
}
CRUDRenderHelper代码:
<?php
namespace App\Helpers\Traits;
use App\Helpers\Exceptions\ValidationException;
use App\Helpers\SessionHelper;
use DB;
use Exception;
use Illuminate\Http\Request;
use Laracasts\Flash\flash;
use Yajra\Datatables\Datatables;
use Illuminate\View\View;
trait CRUDRenderHelper
{
public function render(View $view, $route = null, $obj = null, $method = 'POST') {
$title = $this->title;
$appName = $this->appName;
$action = $this->action;
return $view->with(compact('title', 'appName', 'route', 'obj', 'method', 'action'));
}
public function getFormRoute($obj = null) {
if ($obj) {
$route = property_exists($this, 'updateFormRouteName') ?
$this->updateFormRouteName : $this- >routeBindModel.'.update';
return route($route, [$this->routeBindModel => $obj]);
}
$route = property_exists($this, 'saveFormRouteName') ?
$this->saveFormRouteName : $this->routeBindModel.'.store';
return route($route);
}
public function saveHandler(Request $request, $obj) {
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
public function sendErrorResponse(Request $request, $errors) {
if ($request->ajax()) {
return response($errors, 400);
}
SessionHelper::setMessage($errors, 'danger');
return back()->withInput()->withErrors($errors);
}
public function sendSuccessResponse(Request $request) {
if ($request->ajax()) {
return response('Success', 200);
}
flash('Process Successfully', 'success');
if (property_exists($this, 'redirectPageWhenFormSuccess')) {
return redirect()->route($this->redirectPageWhenFormSuccess);
}
return back();
}
public function save(Request $request) {
return $this->saveHandler($request, $obj);
}
public function delete(Request $request, $obj) {
if (!$obj->delete()) {
throw new ValidationException($obj->errors());
}
return $this->sendSuccessResponse($request);
}
/**
* [datatableBuilder Build model query here]
* @return [Object] [Laravel Eloquent Model]
*/
public function datatableBuilder($obj = null) {}
public function makeDatatable($obj) {
return Datatables::of($obj)
->addColumn('action', function ($model) {
return $this->makeActionButtonsForDatatable($model);
})
->make(true);
}
/**
* [makeActionButtonsForDatatable Make buttons for datatables]
* @param [Object] $model [Eloquent Model]
* @return [string] [HTML script]
*/
public function makeActionButtonsForDatatable($model) {
$routeShow = property_exists($this, 'showRouteName') ?
$this->showRouteName : $this->routeBindModel.'.show';
$routeEdit = property_exists($this, 'editRouteName') ?
$this->editRouteName : $this->routeBindModel.'.edit';
$routeDelete = property_exists($this, 'deleteRouteName') ?
$this->deleteRouteName : $this->routeBindModel.'.destroy';
return '
<a href="'.route($routeEdit, [$this->routeBindModel => $model->id]).'"
class="btn btn-warning btn-xs edit">
<span class="fa fa-pencil"></span> Edit
</a>
<a id="delBtn" data-url="'.route($routeDelete, [$this->routeBindModel => $model->id]).'"
data-toggle="modal" data-target=" #modalDelete" data-title="Confirmation" data-table-name="#datatable" data-message="Do you want to delete this record?"
class="btn btn-danger btn-xs delete" >
<span class="fa fa-trash-o"></span> Delete
</a>';
}
protected function setAction($string) {
$this->action = $string;
return $this;
}
}
任何想法?
答案 0 :(得分:1)
你可能有这样的路线:
Route::post('car_category/{id}', 'CarCategoriesController@save');
您正在接收路线中的ID。默认情况下,它们作为字符串传递给控制器方法。在您的代码中,您将id(字符串)传递给saveHandler()
方法。这就是$obj->fill()
第41行CRUDRenderHelper
抛出异常的原因。
更改save
方法以检查$id
是否为空,如果是,则创建一个新的CarCategory,如果没有,则尝试使用给定的{{1}从数据库中获取它}:
$id