简体   繁体   中英

MethodNotAllowedHttpException in laravel 5 when calling POST

I'm new to Laravel, I'm trying to update a record in my user table but I get this MethodNotAllowedHttpException , I've tried everything but nothing has worked.

UsersController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Users;

class UsersController extends Controller
{
    public function index()
    {
        $users = Users::all();
        return view('admin.users',compact('users'));
    }

    public function block(Request $request)
    {
        $user = Users::find($request->id);
        $user->blocked = 1;
        $user->save();

        return redirect()->action('UsersController@index'); 
    }
}

route.php

Route::get('/admin/users', 'UsersController@index');
Route::post('/admin/users/block',  'UsersController@block');

Until $user->blocked = 1; everything's well .

UPDATE I'm accessing the controller from this jQuery method

$("#valdiate-user-block").click(function(){
    var data = {
        id: 2
    }
    $.ajax({
        url: "block",
        type:"POST",
        data: data,
        success:function(data){
            alert(data);
        },error:function(){ 
            alert("error!!!!");
        }
    }); //end of ajax
    });

i didn't mention it because i localized the error, when I access the controller from the root i get the error and when accessing it from jQuery I get

POST http://localhost:1303/admin/users/block 500 (Internal Server Error)

Please help me i'm stacked !!

If you want update you have to use PATCH consider on that and in your method

public function block(User $user, Request $request)
    {
        $input = $request->all();
        $input['blocked'] = 1;
        $user->update($input);

        return redirect()->action('UsersController@index'); 
    }

if you want use POST then do like this

public function block(Request $request)
        {
            $input = $request->all();
            $input['blocked'] = 1;
            User::create( $input );
            return redirect()->action('UsersController@index'); 
        }

Instead of using below two lines:

use Illuminate\Http\Request;
use App\Http\Requests; // seems this one also incorrect to me (Requests?)(Request !)

Simply use below and check:

use Request;

Your updated code will be:

    namespace App\Http\Controllers;
    use Request;
    use App\Http\Controllers\Controller;
    use App\Users;
    use Illuminate\Support\Facades\Input;

    class UsersController extends Controller
    {
        public function index(){
            $users = Users::all();
            return view('admin.users',compact('users'));
        }

        public function block(){

            $inputData = Input::all(); // Reads all input array
            $your_id=   $inputData['id'];
            $user->blocked = 1;
            $user->save();

            return redirect()->action('UsersController@index');

        }
    }

I fixed it :)

i addedd public $timestamps = false; in the Users class

and i deletet the type from the ajax call

$.ajax({
url: "users/block/",
data: data,
success:function(data){
alert(data);
},error:function(){ 
alert("error!!!!");
}

Thanx for your help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM