0

When I clicked on the add button, a MethodNotAllowedHttpException error appeared. What is wrong in my code?

I have read all of the version of correction but nothing helped me.

This is my getdata() function inside AjaxdataController:

function getdata()
    {
        $users=User::select('id','name','lastname','email','created_at');

        return DataTables::of($users)
        ->addColumn('action', function ($user) {
                return '<a href="#edit-'.$user->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
            })
        ->make(true);
    }

    function postdata (Request $request)
    {
      $validation=Validator::make($request-> all(),[
       'name' =>'required',
       'lastname' => 'required',
       'email' =>'required',
       'password' =>'required'
      ]);
      $error_array= array();
      $success_output='';
      if ($validation->fails())
      {
             foreach ($validation->messages()->getMessages as $field_name => $messages) {

                $error_array[]=$messages;
             }
      } 
      else
      {
            if ($request->get('button_action')=="insert")
            {
                $user =new User([
                       'name' =>$request->get('name'),
                       'lastname'=>$request->get('lastname'),
                       'email'=>$request->get('email'),
                       'password'=>$request->get('password')

                ]);
                $user->save();
                $success_output= '<div class="alert alert-success"> Data Inserted </div>';
            }
      }
       $output=array(
             'error' =>$error_array,
             'success'=>$success_output
         );
       echo json_encode($output);
    }

1 Answers1

0

I assume that you defined the route as a GET request but the form is probably sending a POST request. Change your route definition.

// Change this  
Route::get('/foo', 'FooController@bar')


// To this
Route::post('/foo', 'FooController@bar')