Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.2: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

I am new to laravel and i am doing a project in laravel 5.2 for my study purpose. But i got stuck with some errors when updating the data in the form. The insert, select and delete queries are working fine without any errors. But while using update query it is showing error as given below:

ErrorException in helpers.php line 683: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

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

Below given is the code written by me for the view file of the updation form (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>

Below given is my corresponding function written in the controller for updating the data(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');
        }
    }
}

Below given is the corresponding code i given in the route file (routes.php) to call the corresponding pages:

Route::get('admin/editmenutype/{id}','AdminController@editmenutype');
Route::post('admin/updatemenutype','AdminController@updatemenutype');

Why this error occurring in case of update query only? Please help me with a stable and correct method to fix this issue... Thanks in advance.

like image 708
Mahesh A R Avatar asked Oct 18 '22 15:10

Mahesh A R


1 Answers

array() and \[\] are both declarations of an empty array.

In your AdminController.php, see:

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'],
        ]);
    ...        
}

Instead, it should be:

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'],
        ];
    ...        
}
like image 78
Mysteryos Avatar answered Oct 27 '22 00:10

Mysteryos