[Solved] php laravel 302 error when trying to POST PAI

Murilo Fernandes Asks: php laravel 302 error when trying to POST PAI
a 302 error is returned when i’m trying to post to API Route, only in the second Post, using the function insereTelefone. When i’m using the Postman, it’s works properly, so i’m thinking the problem is with Route, but I don’t know what. I’m a newbie at the Laravel, so i’m learning how to implement the things.

Here is my code:

Code:
namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use IlluminateHttpRedirectResponse;




class IndexClientes extends Controller
{
    //

    public function index(){

       

        $request = Request::create('/api/clientes', 'GET');
        $response = Route::dispatch($request);
        $clientes = json_decode($response->getContent(), true);

        
        

        return view('index', compact('clientes'));

    }

    public function create() {

        return view('formulariocliente');

    }
    
    public function store(Request $request) {

        $nome = $request->nome;
        $cpf = $request->cpf;
        $email = $request->email;
        $numerosTelefone = $request->telefone;
        $tiposTelefone = $request->tipoTelefone;

        $request = Request::create('/api/clientes', 'POST', array(
            "nome"     => $nome,
            "cpf"    => $cpf,
            "email"    =>  $email
           
        ));           
        $responseInicial = Route::dispatch($request);

        $response = json_decode($responseInicial->getContent(),true);

        $status = json_decode($responseInicial->status(),true);

        

        if ($status !== 200) :

            echo "ERRO";
            die();

        endif;



        $idCliente = $response['id'];

       if(!empty($numerosTelefone) ) :

        

            $i = 0;

            foreach ($numerosTelefone as $numeroTelefone) :

                $tipoTelefone = (int)$tiposTelefone[$i];
                $numeroTelefone = (int)$numeroTelefone;

                  
                if(!empty($tipoTelefone) && !empty($numeroTelefone) ) :

                    
                    return self::insereTelefone($idCliente, $tipoTelefone, $numeroTelefone);
                    

                endif;
                
                $i++;


            endforeach;

        endif;

        

        

        
        

    }

    public function insereTelefone($idCliente, $tipoTelefone, $numTelefone) {

        $array = array(
            "cliente_id"     => $idCliente,
            "telefone_tipo_id"    => $tipoTelefone,
            "numero"    =>  $tipoTelefone
        
        );

        $request = Request::create('api/telefones', 'POST', $array ); 

        $responseInicial = Route::dispatch($request);


        $response = json_decode($responseInicial->getContent(),true);

        $status = json_decode($responseInicial->status(),true);

        return $status;

    }
}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.