Angular form with laravel

0

I have two tables: Invitations and Process.

A process can have an invitation, just 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 it is.

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

The select normally loads the data of the registered processes.

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

But I tried every way known (for me) to save in the bank and I can not, it does not save and it does not give me the error return, simply when I click the save button it does nothing.

I'm starting at angular, and I use it along with laravel, could you tell me or indicate a tutorial where it is shown the correct way to save the data of an angle.

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

Thanks for the 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 made to save the data in the table, it worked until I related the table and include the process_id field in the invitation table.

I also did the methods in the 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 angular, and I adapted a code that I got ready, as I said before making the relationship worked.

// Controller

app.controller ('InvitationsController', function ($ scope, invitationService) {     $ scope.listar = function () {         invitationService.list (). success (function (data) {             $ scope.convites = data;         });

    // 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, it was a basic tag closing error, but now I can not 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 InvitationsController.

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

    
asked by anonymous 24.01.2017 / 15:40

0 answers