dmelo320's avatar

status 302 when submitting form

Hello, I'm using laravel 5.4 and when I submit the registration form, I get status 302 and it is not sent to the database and I'm going back to the form page.

Controller

 /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
         $estados = comboUF();
        return view('site.teste.create', ['uf' => $estados]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
     $request->merge( array('senha' => bcrypt($request->senha )) );
     //$request->merge( array('nascimento' => converterData($request->nascimento, 'd/m/Y')->format('Y-m-d')) );


     //$cliente = $this->cliente->create($request->all());    
     $cliente = $request->all();

     if($cliente){            

        $mensagem = array(
            'type' => 'success',
            'retorno' => 'Cliente cadastrado com sucesso'
        );
    }else{

        $mensagem = array(
            'type' => 'error',
            'retorno' => 'Erro ao salvar'
        );

    }
    return redirect()->route('site.acesso')
    ->with('status', $mensagem['type'])
    ->with('retornomensagem', $mensagem['retorno']);
}


Routes


Route::get('cliente/create/site', ['as' => 'site.cliente.create', 'uses' => 'Site\ClienteSiteController@create']);

Route::post('cliente/cadastro', ['as' => 'site.cliente.post', 'uses' => 'Site\ClienteSiteController@store']);


view form



<form method="post" action="{{url('cliente/cadastro')}}" class="form-validate">
{{ csrf_field() }}

                

0 likes
5 replies
MiguelBarros's avatar

Hi , in first place that merge doens´t make any sense in my opinion, second the only line that would allow you to insert record on database is commented.

Do you have a Client model ? if so do this way


public function store(Request $request)
    {
    $cliente = Client::create([
        'name_for_example' => request()->input('name'),
        'email_for_example' => request()->input('email'),
        'password_for_example' => bcrypt(request()->input('password')) 
    ]);

    // Or You can do 

    $cliente = new Client;
    $client->name_for_example = request()->input('name');
    $client->email_for_example = request()->input('email');
    $client->password_for_example = request()->input('password');

     if($cliente){            

        $mensagem = array(
            'type' => 'success',
            'retorno' => 'Cliente cadastrado com sucesso'
        );
    }else{

        $mensagem = array(
            'type' => 'error',
            'retorno' => 'Erro ao salvar'
        );

    }
    return redirect()->route('site.acesso')
    ->with('status', $mensagem['type'])
    ->with('retornomensagem', $mensagem['retorno']);
}

1 like
dmelo320's avatar

Model Client

<?php

namespace App;

use App\Newsletter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Cliente extends Authenticatable
{
    use SoftDeletes;
    
    protected $fillable = ['nome','sobrenome','nascimento', 'cpf','email','telefone','cep','endereco','complemento', 'cidade', 'uf', 'senha', 'numero', 'bairro'];
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['deleted_at'];

    
    public function getAuthPassword() {
        return $this->senha;
    }


    public function enderecos()
    {
        return $this->hasMany('App\EnderecoCliente', 'endereco_clientes', 'id', 'id_cliente');
    }

    public function pedidos()
    {
        return $this->hasMany('App\Pedido', 'pedidos', 'id', 'id_cliente');
    }

    public function documento()
    {
        return $this->hasOne('App\ClienteDocumento', 'id_cliente', 'id');
    }


    public function opcoesSelect()
    {       
        $data = Cliente::orderBy('nome', 'asc')->get();
        $opcoes = array();

        foreach ($data as $cliente) {
            $opcoes[$cliente->id] = $cliente->nome;
            
        }   
        return $opcoes;
    }


    /**
     * Verifica se Cliente consta em Newsletter
     * @return boolean retorna verdadeiro ou 1 se existe cadastro, false do contrario
     */
    public function hasNewsletter()
    {
        $news = Newsletter::where('email', $this->email)
                            ->first();
        if($news != null) return $news->status;
        return false;
    }
}
robrogers3's avatar

First:

neither this nor this will save anything:;

    $cliente = new Client;
    $client->name_for_example = request()->input('name');
    $client->email_for_example = request()->input('email');
    $client->password_for_example = request()->input('password'); 
    //you need save.
    $client->save(); //yep save it.


 $cliente = $request->all(); //you are just assigning the request->all() to $cliente

3rd where is this route

route('site.acesso')

4th are you using the auth middleware? and if so, did you do

$ php artisan make::auth
2 likes
dmelo320's avatar
dmelo320
OP
Best Answer
Level 1

Problem solved! thanks! :D

use Illuminate\Http\Request;


Route::get('clientche', ['as' => 'site.cliente.index', 'uses' => 'Manager\ClienteSiteController@inicio']);
//Route::get('cliente/loo', ['as' => 'site.cliente.edit', 'uses' => 'Site\ClienteSiteController@update']);
Route::get('cliente/create/site', ['as' => 'site.cliente.create', 'uses' => 'Site\ClienteSiteController@create']);

Route::post('cliente/cadastro', ['as' => 'cliente.cad', function(Request $request) {

//  protected $fillable = ['id_cliente', 'tipo', 'cpfcnpj', 'ie', 'isento'];
    $request->merge( array('nascimento' => converterData($request->data, 'd/m/Y')->format('Y-m-d')) );
    $reg = $request->all();
    
    $cliente = App\Cliente::create($reg);
    if(isset($reg['cpf'])) {
        App\ClienteDocumento::create([
            'id_cliente' => $cliente->id,
            'tipo' => 'F',
            'cpfcnpj' => $request->input('cpf')
        ]);
    } else {
        App\ClienteDocumento::create([
            'id_cliente' => $cliente->id,
            'tipo' => 'J',
            'cpfcnpj' => $request->input('cnpj'),
            'ie' => $request->input('inscricao-estadual'),
            'isento' => $request->input('isento') == "on" ? 1 : 0,
        ]);
        
    }
    return redirect('/');
}]);

Please or to participate in this conversation.