我是laravel的新手,为了学习目的,我正在laravel 5.2做一个项目。但是在更新表单中的数据时我遇到了一些错误。插入,选择和删除查询工作正常,没有任何错误。但是在使用更新查询时,它显示的错误如下所示:
helpers.php中的ErrorException第683行:preg_replace():参数不匹配,pattern是字符串,而替换是数组
in helpers.php line 683
at HandleExceptions->handleError('2', 'preg_replace(): Parameter mismatch, pattern is a string while replacement is an array', 'F:\xampp\htdocs\multidatatechnologies.com\vendor\laravel\framework\src\Illuminate\Support\helpers.php', '683', array('search' => '\?', 'replace' => array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), 'subject' => 'update `menutypes` set `0` = ? where `id` = ?', 'value' => array('menutype' => 'Fast Food', 'status' => '1')))
at preg_replace('/\?/', array('menutype' => 'Fast Food', 'status' => '1'), 'update `menutypes` set `0` = ? where `id` = ?', '1') in helpers.php line 683
at str_replace_array('\?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), 'update `menutypes` set `0` = ? where `id` = ?') in QueryException.php line 56
at QueryException->formatMessage('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), object(PDOException)) in QueryException.php line 39
at QueryException->__construct('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), object(PDOException)) in Connection.php line 653
at Connection->runQueryCallback('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), object(Closure)) in Connection.php line 611
at Connection->run('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2'), object(Closure)) in Connection.php line 416
at Connection->affectingStatement('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2')) in Connection.php line 359
at Connection->update('update `menutypes` set `0` = ? where `id` = ?', array(array('menutype' => 'Fast Food', 'status' => '1'), '2')) in Builder.php line 1904
at Builder->update(array(array('menutype' => 'Fast Food', 'status' => '1'))) in AdminController.php line 84
at AdminController->updatemenutype(object(Request))
at call_user_func_array(array(object(AdminController), 'updatemenutype'), array(object(Request))) in Controller.php line 76
at Controller->callAction('updatemenutype', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(AdminController), object(Route), 'updatemenutype') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(AdminController), object(Route), object(Request), 'updatemenutype') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\AdminController', 'updatemenutype') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
下面给出的是我为更新表格的视图文件(editmenutype.blade.php)编写的代码:
<form class="form-horizontal" method="post" action="{{action('AdminController@updatemenutype')}}">
<input type="hidden" name="_token" value="{{csrf_token()}}" />
<input type="hidden" name="eid" value="{{$row->id}}" />
<div class="box-body">
<label>Menu-Type Name:</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-fire"></i>
</div>
<input type="text" name="menutype" class="form-control" value="{{$row->menutype}}" placeholder="Enter Menu-Type Name..." required="required" maxlength="100">
</div><!-- /.input group -->
</div><!-- /.form group -->
<div class="box-body">
<label>Publishing Status:</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-globe"></i>
</div>
<select name="status" class="form-control" style="width: 100%;" required="required">
<option value="{{$row->status}}" disabled="disabled"><?php if($row->status==1){echo "Yes"; }else{echo "No";} ?></option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div><!-- /.input group -->
</div><!-- /.form group -->
<div class="box-footer">
<input type="reset" name="reset" class="btn btn-default" value="Cancel">
<input type="submit" name="submit" class="btn btn-info pull-right" value="Edit Menu-Type">
</div>
</form>
下面给出了我在控制器中编写的用于更新数据的相应函数(AdminController.php):
public function editmenutype($id) {
$row = DB::table('menutypes')->where('id',$id)->first();
return view('admin.editmenutype')->with('row',$row);
}
public function updatemenutype(Request $request) {
$post=$request->all();
print_r($post);
//var_dump($post);
$v=\Validator::make($request->all(),
[
'menutype' => 'required',
'status' => 'required',
]);
if($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
else
{
$data = array(
[
'menutype' => $post['menutype'],
'status' => $post['status'],
]);
$i = DB::table('menutypes')->where('id',$post['eid'])->update($data);
if($i > 0)
{
\Session::flash('message','* Menu-Type Updated Successfully.');
return redirect('/admin/managemenutypes');
}
}
}
下面给出的是路由文件(routes.php)中给出的相应代码,用于调用相应的页面:
Route::get('admin/editmenutype/{id}','AdminController@editmenutype');
Route::post('admin/updatemenutype','AdminController@updatemenutype');
为什么仅在更新查询时才会出现此错误?请帮助我找到一个稳定而正确的方法来解决这个问题...提前致谢。
答案 0 :(得分:3)
array()
and \[\]
are both declarations of an empty array.
在AdminController.php
中,请参阅:
public function updatemenutype(Request $request) {
...
if($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
else
{
//Error is here: array within array
$data = array(
[
'menutype' => $post['menutype'],
'status' => $post['status'],
]);
...
}
相反,它应该是:
public function updatemenutype(Request $request) {
...
if($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
else
{
//Single array declaration
$data =
[
'menutype' => $post['menutype'],
'status' => $post['status'],
];
...
}