Angular form with Laravel

Asked

Viewed 307 times

0

I have two tables: Invitations and Process.

A process can have an invitation as well as an invitation belongs to a process.

In this way I made the relationship in the database where the Invitation table has the process_id field referencing which process is.

So I register the processes, and when I want to register an invitation I use the combobox below to choose which process is the invitation.

select normally loads data from registered processes.

I even put the tag to show the process id just to know which id is referencing.

but I’ve tried every way known (for me) to save in the bank and I can’t, it doesn’t save and don’t give me error return, simply when I click save button it does nothing.

I am starting in angular, and I use it together with the Standard, could tell me or indicate a tutorial where it is shown which is the correct way to save the data of an angular.

 <div ng-controller="procconvitController">
<div class="form-group">
    <label>Nº. Processo:</label>
    <select ng-model="convite.processo_id" ng-options="processo.id as processo.numero for processo in processos">
    </select> <br>  
    <small><% convite.processo_id %></small>
    <input type="text" class="form-control" value="<% convite.processo_id %>">
</div>

Thank you for your attention

<button type="button" class="btn btn-primary" ng-click="salvar()">Salvar</button>

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

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

});

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

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

this is all the way done to save the data in the table, it worked until I list the table and include the processo_id field in the invitation table.

I also did the methods on models:

class Convites extends Model
{


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

}

class Processo extends Model
{
     public function convite()
    {
        return $this->belongsTo('Convite');
    }
}

trying again I noticed some errors in the console

angular.js:13708 TypeError: Cannot read property 'id' of undefined
at Scope.$scope.salvar (app.js:49)
at fn (eval at compile (angular.js:14605), <anonymous>:4:209)
at expensiveCheckFn (angular.js:15694)
at callback (angular.js:25622)
at Scope.$eval (angular.js:17444)
at Scope.$apply (angular.js:17544)
at HTMLButtonElement.<anonymous> (angular.js:25627)
at HTMLButtonElement.dispatch (app.js:3)
at HTMLButtonElement.g.handle (app.js:3)

(Anonymous) @angular.js:13708

@radamesrf

this is all my code, I know very little of angular, and I adapted a code I took ready, as I said before to make the relationship work.

// Controller

app.controller('convitesController', Function($Scope, conviteService) { $Scope.listar = Function(){ conviteService.lista(). Success(Function(data){ $Scope.invitations = date; });

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

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

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

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

});

one of the problems I solved, was a basic tag closing error, but now I can’t pass the data from one controller to another, because if I put the input outside the procconvitController it does not take the data, but also with the input inside it does not pass to the convitesController.

I solved the problem of passing the data, I used #rootscope

  • The problem is in "if($Scope.convite.id){" in your save method. Is the invitation in your scope set to "id"? How are you initiating this "$Scope."?

  • I don’t know what to do outside this @radamesrf, because I used a model code, I’m still learning about angular.

No answers

Browser other questions tagged

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