Search code examples
phplaravelpostlaravel-5put

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 !!


Solution

  • 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 :)