Http post method, date parameter is always empty (Angularjs + Laravel 5.2)

0

I'm using angularjs + laravel to build my application. The question is, I have a $ scope.frequencias, which contains some information on the frequency of a student. I did a foreach angle to send this data to the bank, but my date parameter (which contains this scope) is always going to be empty.

Foreach:

angular.forEach($scope.frequencias, function(value, i, frequencias){
      $scope.frequencia = [];

      $scope.frequencia = frequencias[i];
      url = frequencias[i].url;
      
      //Aqui é mostrado o scope que quero enviar para o banco, está correto
      console.log($scope.frequencia);
      //aqui é mostrada a url para o metodo, que também está correta
      console.log(url);

      
      $http({
              method: 'POST',
              url: url,
              data: $.param($scope.frequencia),
              headers: {'Content-Type': 'application/x-www-form-urlencoded'}
              }).success(function(response) {
                  console.log(response);
              }).error(function(response) {
                  console.log(response);
                  alert('Um erro ocorreu. Check a log para mais detalhes.');
              }); 
              
    })

The laravel controller, which receives the scope and inserts into the bank

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controler;
use App\Frequencia;
use Carbon\Carbon;

class Frequencias extends Controller
{
	public function salvar(Request $request)
	{
			
			$frequencia = new Frequencia;

	        $frequencia->data_frequencia = 
              $request->input('data_frequencia');
	        $frequencia->numero_faltas = 
              $request->input('numero_faltas');
			$frequencia->aluno_id = 
              $request->input('aluno_id');
			$frequencia->disciplina_id = 
              $request->input('disciplina_id');
		

	        $frequencia->save();
	        
	}

	public function update(Request $request)
	{
		
		$id = $request->input('id');

		
		$frequencia = Frequencia::find($id);

        $frequencia->data_frequencia = 
          $request-  >input('data_frequencia');
        $frequencia->numero_faltas = 
          $request->input('numero_faltas');
		$frequencia->aluno_id = 
          $request->input('aluno_id');
		$frequencia->disciplina_id = 
          $request->input('disciplina_id');
	

        $frequencia->save();
        
		
	}
}

The routes used for laravel

//rotas para frequencia

Route::post("/api/v1/frequencias/salvar","Frequencias@salvar");

Route::post("/api/v1/frequencias/atualizar","Frequencias@update");

By the time we arrive at the controller, it tries to enter the parameters to null, and the error in the bank, because some attributes are uniques. I insert it this way in several points of my application, and only in this one that is giving problem. Thanks for the help right away.

    
asked by anonymous 21.10.2016 / 18:43

1 answer

2

Verify that the "frequency_time" parameter is indeed being sent. Since I generally use chrome, in Developer Tools - > Network has its POST, and is sending some value.

And a hint, use the value variable instead of the array, for example.

 angular.forEach($scope.frequencias, function(frequencia, index){

    $http({
        method: 'POST',
        url: frequencia.url,
        data: $.param(frequencia),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response) {
        console.log(response);
    }).error(function(response) {
        console.log(response);
        alert('Um erro ocorreu. Check a log para mais detalhes.');
    }); 

})
    
21.10.2016 / 19:10