Errors with Put and Delete methods - Methodnotallowedhttpexception - Laravel and Angular

Asked

Viewed 658 times

1

Hello, follow my fight with angular and Laravel.

Now I’m having trouble with put and delete methods, I did nothing but what I had before in my code, I’m just adding more models - where I copy the previous code and change the names.

But while trying to edit or delete some record, I get this error

MethodNotAllowedHttpException in the Laravel

And by the console

DELETE http://localhost:8000/business/excludes/1 500 (Internal Server Error) (Anonymous) @ angular.js:11881 sendReq@ angular.js:11642 serverRequest @ angular.js:11352 processQueue @ angular.js:16170 (Anonymous) @ angular.js:16186 $Eval @ angular.js:17444 $Digest @ angular.js:17257 $apply @ angular.js:17552 (Anonymous) @angular.js:25627 Dispatch @ app.js:3 g.Handle @ app.js:3

I’ve tried some things similar errors I’ve seen here on the forum, but nothing so far, and I don’t know what could be, because my code was working normally.

I don’t know where the error might be so I’ll post some parts, if necessary request that I include more.

PS: the only difference now for previous models is that this one has an N/N relationship, but the previous model that is 1/N also started to give error.

Sincerely yours.

namespace confin;

use Illuminate\Database\Eloquent\Model;

class Empresa extends Model
{

    public function processo()
    {
        return $this->belongsToMany('confin\Processo','empresa_processo');
    }


}

    class Processo extends Model {

    public function convites()

    {

        return $this->hasOne('confin\Convites');
    }


    public function empresa()
    {
        return $this->belongsToMany('confin\Empresa','empresa_processo');
    }

}


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
     <div class="modal-content">
        <div class="modal-header">
           <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
           <h4 class="modal-title" id="myModalLabel">Cadastro de Número de processo</h4>
        </div>
        <div class="modal-body">

            <div class="form-group">
              <input type="hidden" class="form-control" ng-model="processo.ano" >
              <label>Ano:</label>
              <input type="text" class="form-control" ng-model="processo.ano">
           </div>

           <div class="form-group">
              <label>Nº. Processo:</label>
              <input type="text" class="form-control" ng-model="processo.numero">
           </div>

           <label>Descrição:</label>
           <div class="form-group">
              <textarea  class="form-control" ng-model="processo.descricao"rows="10" cols="500"></textarea>
           </div>

        </div>

        <div class="modal-footer">
           <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="processo = {}">Cancelar</button>
           <button type="button" class="btn btn-primary" ng-click="salvar()">Salvar</button>
        </div>
     </div>
  </div>
</div>



app.factory('processoService',function($http) {
    return {
        lista: function(){
            return $http.get('/processos/lista');
        },
        cadastra: function(data){
            return $http.post('/processos/cadastra', data);
        },
        edita: function(data){
            var id = data.id;
            delete data.id;
            return $http.put('/processos/edita/'+id, data);
        },
        exclui: function(id){
            return $http.delete('/processos/exclui/'+id)
        }
    }
});



// Controller
app.controller('processosController', function($scope, processoService, empresaService) {
    $scope.listar = function(){
        processoService.lista().success(function(data){
            $scope.processos = data;
        });

        // ordenar
        $scope.ordenar = function(keyname){
        $scope.sortKey = keyname;
        $scope.reverse = !$scope.reverse;
        };
    }


    $scope.editar = function(data){
        $scope.processo = data;
        $('#myModal').modal('show');
    }

    $scope.salvar = function(){
        alert(processo.id);
        if($scope.processo.id){
            processoService.edita($scope.processo).success(function(res){
                $scope.listar();
                $('#myModal').modal('hide');
            });
        }else{
            processoService.cadastra($scope.processo).success(function(res){
                $scope.listar();
                $('#myModal').modal('hide');
            });
        }
    }

    $scope.excluir = function(data){
        if(confirm("Tem certeza que deseja excluir?")){
            processoService.exclui(data.id).success(function(res){
                $scope.listar();
            });
        }
    }
});




namespace confin\Http\Controllers;

use Illuminate\Http\Request;

use DB; // para usar a classe DB

class ProcessoController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    // Index
    public function index()
    {
        return view('processo');
    }

    // Listando processo
    public function lista()
    {
        return DB::table('processos')
            ->get();
    }


    // Cadastrando Processo
    public function novo(Request $request)
    {
        $data = sizeof($_POST) > 0 ? $_POST : json_decode($request->getContent(), true); // Pega o post ou o raw

        return DB::table('processos')
            ->insertGetId($data);
    }


    // Editando processo
    public function editar($id, Request $request){
        $data = sizeof($_POST) > 0 ? $_POST : json_decode($request->getContent(), true); // Pega o post ou o raw

        $res = DB::table('processos')
            ->where('id',$id)
            ->update($data);

        return ["status" => ($res)?'ok':'erro'];
    }


    // Excluindo Processo
    public function excluir($id)
    {
        $res = DB::table('processos')
            -> where('id',$id)
            -> delete();

        return ["status" => ($res)?'ok':'erro'];
    }
}

DELETE registration/process/{id} confin Http Controllers Processocontroller@delete web,auth

PUT registration/process/{id} confin Http Controllers Processocontroller@edit web,auth

POST registration/processes confin Http Controllers Processocontroller@new web,auth

GET|HEAD | register/processes confin Http Controllers Processocontroller@web list,auth

  • Ask your route file (I think what matters most about the question next to your process).

2 answers

0

This is certainly problems with routes.

1st - Search in php artisan route:list if the route empresas/exclui/{id} is defined and with DELETE method

2º - If you are not putting the line Route::Resource/get/post/delete from your Processes file Routes.php for me to take a look.

  • Apparently there is no mistake, at least for me, the strange, is that if I try to edit and delete from the error, now if I try to delete will, and if I edit from the error, F5 grip and try to delete will. I don’t know if I understand

  • DELETE |cadastro/empresa/{id} confin\Http\Controllers\EmpresaController@excluir web,auth &#xA; PUT|cadastro/empresa/{id}|confin\Http\Controllers\EmpresaController@editar web,auth &#xA; POST|cadastro/empresas|confin\Http\Controllers\EmpresaController@novo web,auth GET|HEAD|register/business|confin Http Controllers Companyntroller@web list,auth

  • I found the error when switching this line Invitations::with('Process')->get(); for this DB::table('invitations')->get(); I know it’s in the relationship, but not why.

0

Could be the route cache

php artisan route:cache

and then search your route

php artisan route:list --name=nome_da_sua_rota

and there you can check for sure if your route exists and this with the correct method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.