My form does not save the data in the database

Asked

Viewed 45 times

1

dry my view:

{!! Notification::showAll() !!}
{!! Form::open() !!}

  <div class="panel panel-default">
  <div class="panel-heading"><h2> Cadastro Escola </h2></div>
  <div class="panel-body">
  <hr/>
       <div class="row">
       <div class="col-sm-3">
            {!! Form::label('inep', 'INEP*:') !!} <br/>
            {!! Form::text('inep',null,['class'=> 'form-control', 'readonly']) !!}
            {!! $errors->first('inep', '<span class="text-danger">:message</span> ') !!}
          </div>
          <div class="col-sm-6">
            <div class="form-group">
                  {!! Form::label('nome', 'Nome Escola*:') !!} <br/>
                  {!! Form::text('nome',null,['autofocus','class'=> 'form-control buscaEscola', 'placeholder' => 'Busque pelo nome da escola']) !!}
                  {!! $errors->first('nome', '<span class="text-danger">:message</span> ') !!}
                  <div class="message-box"></div>
            </div>
          </div>
          <div class="col-sm-3">
            {!! Form::label('rede_id', 'Rede de Ensino*:') !!} <br/>
            {!! Form::select('rede_id',$redes,null,['class'=> 'form-control']) !!}
            {!! $errors->first('rede_id', '<span class="text-danger">:message</span> ') !!}

          </div>
        </div>
        <br/>
        <div class="row">
          <div class="col-sm-3">
            {!! Form::label('cnpj', 'CNPJ*:') !!} <br/>
            {!! Form::text('cnpj',null,['class'=> 'form-control mask-cnpj']) !!}
            {!! $errors->first('cnpj', '<span class="text-danger">:message</span> ') !!}
          </div>
          <div class="col-sm-9">
            {!! Form::label('razao_social', 'Razão Social*:') !!} <br/>
            {!! Form::text('razao_social',null,['class'=> 'form-control']) !!}
            {!! $errors->first('razao_social', '<span class="text-danger">:message</span> ') !!}
          </div>
       </div>

  <h3>Endereço:</h3>

    <div class="row">
        <div class="col-sm-5">
        {!! Form::label('logradouro', 'Logradouro*:') !!} <br/>
        {!! Form::text('logradouro',null,['class'=> 'form-control']) !!}
        {!! $errors->first('logradouro', '<span class="text-danger">:message</span> ') !!}
      </div>
      <div class="col-sm-2">
        {!! Form::label('numero', 'Número*:') !!} <br/>
        {!! Form::text('numero',null,['class'=> 'form-control']) !!}
        {!! $errors->first('numero', '<span class="text-danger">:message</span> ') !!}
      </div>
      <div class="col-sm-5">
        {!! Form::label('bairro', 'Bairro*:') !!} <br/>
        {!! Form::text('bairro',null,['class'=> 'form-control']) !!}
        {!! $errors->first('bairro', '<span class="text-danger">:message</span> ') !!}
      </div>
    </div>
    <br/>

      <div class="col-sm-4">
        {!! Form::label('municipio', 'Municipio:') !!}
                {!! Form::select('municipio',$municipios,null,['class'=>'form-control ']) !!}
                {!! $errors->first('municipio', '<span class="text-danger">:message</span> ') !!}
      </div>

      <div class="col-sm-4">
        {!! Form::label('regional', 'Regional:') !!}
                {!! Form::select('regional',$regionais,null,['class'=>'form-control ']) !!}
                {!! $errors->first('regional', '<span class="text-danger">:message</span> ') !!}
      </div>


      <div class="col-sm-2">
        {!! Form::label('cep', 'CEP*:') !!} <br/>
        {!! Form::text('cep',null,['class'=> 'form-control mask-cep'])!!}
        {!! $errors->first('cep', '<span class="text-danger">:message</span> ') !!}
      </div>

      <div class="col-sm-2">
        {!! Form::label('telefone', 'Telefone*:') !!} <br/>
        {!! Form::text('telefone',null,['class'=> 'form-control mask-telefone']) !!}
        {!! $errors->first('telefone', '<span class="text-danger">:message</span> ') !!}
      </div>

    </div>
<br>
<div class="row">
    <div class="col-sm-8">
        {!! Form::label('email', 'E-mail Institucional:') !!} <br/>
        {!! Form::email('email',null,['class'=> 'form-control']) !!}
        <br>
    </div>

    </div>

</div>
  <h3>Dados Representante da Escola:</h3>

    <div class="row">
      <div class="col-sm-2">
        {!! Form::label('representante_cpf', 'CPF / Login*:') !!} <br/>
        {!! Form::text('representante_cpf',null,['class'=> 'form-control mask-cpf']) !!}
        {!! $errors->first('representante_cpf', '<span class="text-danger">:message</span> ') !!}
      </div>
      <div class="col-sm-8">
        {!! Form::label('representante_nome', 'Nome*:') !!} <br/>
        {!! Form::text('representante_nome',null,['class'=> 'form-control']) !!}
        {!! $errors->first('representante_nome', '<span class="text-danger">:message</span> ') !!}
      </div>
      <div class="col-sm-2">
        {!! Form::label('representante_funcao', 'Função*:') !!} <br/>
        {!! Form::select('representante_funcao',array('Diretor' => 'Diretor', 'Vice Diretor' => 'Vice Diretor'),null,['class'=> 'form-control']) !!}
        {!! $errors->first('repreentante_funcao', '<span class="text-danger">:message</span> ') !!}
      </div>
    </div>
    <br/>
    <div class="row">
      <div class="col-sm-2">
        {!! Form::label('representante_rg', 'RG:') !!} <br/>
        {!! Form::text('representante_rg',null,['class'=> 'form-control']) !!}
      </div>
      <div class="col-sm-4">
        {!! Form::label('representante_telefone', 'Telefone*:') !!} <br/>
        {!! Form::text('representante_telefone',null,['class'=> 'form-control mask-telefone']) !!}
        {!! $errors->first('representante_telefone', '<span class="text-danger">:message</span> ') !!}
      </div>
      <div class="col-sm-6">
        {!! Form::label('representante_email', 'E-mail*:') !!} <br/>
        {!! Form::email('representante_email',null,['class'=> 'form-control']) !!}
        {!! $errors->first('representante_email', '<span class="text-danger">:message</span> ') !!}
      </div>
    </div>
    <br/>
    <div class="row">
        <div class="col-sm-6">
          <h3>Defina a senha de acesso ao sistema:</h3>
        </div>
        <div class="col-sm-3">
          {!! Form::label('senha', 'Senha*:') !!} <br/>
          {!! Form::password('senha', ['class' => 'form-control', 'placeholder' => 'Senha']) !!}
          {!! $errors->first('senha', '<span class="text-danger">:message</span> ') !!}
        </div>
        <div class="col-sm-3">
          {!! Form::label('senha_confirmation', 'Confirme Senha*:') !!} <br/>
          {!! Form::password('senha_confirmation', ['class' => 'form-control ', 'placeholder' => 'Senha']) !!}
          {!! $errors->first('senha_confirmation', '<span class="text-danger">:message</span> ') !!}
        </div>
    </div>
  <br>
    <div class="well well-sm">
        <a href="{{url("auth/login")}}" class="btn btn-default " >Voltar</a>
      {!! Form::submit('Enviar',['class'=>'btn btn-primary pull-right']) !!}

    </div>

{!! Form::close() !!}

this is my route:

Route::controller('auth', 'AuthController');

that’s mine controller:

public function getCadastro(Request $request)
{
    $v['title'] = 'Cadastro de Escola';
    $v['redes'] = Rede::lists('nome', 'id');
    $v['municipios'] = Municipio::lists('nome','id');
    $v['regionais'] = Regional::lists('nome', 'id');


   return view('auth.cadastro', $v);

}

public function postCadastro(CadastroRequest $request)
{
    $escola = new Escola;
    $escola->fill($request->all());

    try {
            $escola->save();
            $usuario = new Usuario;
            $request->request->add(['perfil_id' => Perfil::ESCOLA]);
            $request->request->add(['escola_id' => $escola->id]);
            $request->request->add(['cpf' => $request->representante_cpf]);
            $request->request->add(['nome' => $request->representante_nome]);
            $request->request->add(['funcao' => $request->representante_funcao]);
            $request->request->add(['rg' => $request->representante_rg]);
            $request->request->add(['telefone' => $request->representante_telefone]);
            $request->request->add(['email' => $request->representante_email]);
            $usuario->fill($request->all());

            try {
                $usuario->save();
                Notification::success('Escola cadastrada com sucesso');
                Notification::success('Novo usuário cadastrado com sucesso');

            } catch (\Exception $e) {
                $escola->delete();
                Notification::error($e->getMessage());
                return back()->withInput();
            }
    } catch (\Exception $e) {
        Notification::error($e->getMessage());
        dd($e);
        return back()->withInput();
    }
    return back();
}

model user

    <?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use Hash;

class Usuario extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    use SoftDeletes;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'usuarios';

    protected $dates = ['created_at','updated_at','deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id','nome', 'cpf', 'senha','email','perfil_id','municipio_id','regional_id','escola_id'];

    protected $loginPath = '/';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['senha', 'remember_token'];

    public function getAuthIdentifierName()
    {
        return $this->id;
    }

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

    public function setSenhaAttribute($value)
    {
        $this->attributes['senha'] = bcrypt($value);
    }

    public function createdBy()
    {
        return $this->belongsTo('App\Usuario','created_by');
    }

    public function updatedBy()
    {
        return $this->belongsTo('App\Usuario','updated_by');
    }

    public function deletedBy()
    {
        return $this->belongsTo('App\Usuario','deleted_by');
    }

    public function perfil()
    {
        return $this->belongsTo('App\Perfil');
    }

    public function municipio()
    {
        return $this->belongsTo('App\Municipio');
    }

    public function regional()
    {
        return $this->belongsTo('App\Regional');
    }

    public function escola()
    {
        return $this->belongsTo('App\Escola');
    }

    public function isAdmin(){

        return $this->perfil_id == \App\Perfil::ADMINISTRADOR;
    }

    public function isEscola(){

        return $this->perfil_id == \App\Perfil::ESCOLA;
    }

    public function isCoordenadorMunicipal(){

        return $this->perfil_id == \App\Perfil::COORDENADOR_MUNICIPAL;
    }

    public function isCoordenadorRegional(){

        return $this->perfil_id == \App\Perfil::COORDENADOR_REGIONAL;
    }


}

model school

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DB, Session;

class Escola extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'cnpj','razao_social','nome','inep','rede_id','logradouro','numero','bairro',
        'municipio_id', 'regional_id','cep','telefone','email','representante_cpf','representante_nome',
        'representante_funcao', 'representante_rg', 'representante_telefone', 'representante_email', 'regional_id'
    ];

    protected $dates = ['created_at','updated_at','deleted_at'];

    public function createdBy()
    {
        return $this->belongsTo('App\Usuario','created_by');
    }

    public function updatedBy()
    {
        return $this->belongsTo('App\Usuario','updated_by');
    }

    public function deletedBy()
    {
        return $this->belongsTo('App\Usuario','deleted_by');
    }

    public function rede()
    {
        return $this->belongsTo('App\Rede');
    }

    public function municipio()
    {
        return $this->belongsTo('App\Municipio');
    }

    public function equipes()
    {
        return $this->hasMany('App\Equipe');
    } 

    public function dirigenteFases() //se o dirigente for técnico será vinculado a uma escola
    {
        return $this->hasMany('App\DirigenteFase');
    }

    public function usuarios()
    {
        return $this->hasMany('App\Usuario');
    }

    public function alunos()
    {
        return $this->hasMany('App\Aluno');
    }

    public function tecnico()
    {
        return $this->hasMany('App\Dirigente');
    }

    public function getCreatedAtAttribute($value){
        return date("d/m/Y", strtotime($value));
    }

    public function getUpdatedAtAttribute($value){
        return date("d/m/Y", strtotime($value));
    }

    public function selectList($municipio_id=null)
    {

        $list = $this->join('municipios','municipios.id','=','escolas.municipio_id')
                     ->orderBy('escolas.nome')
                     ->select(DB::raw("CONCAT(escolas.inep, ' ',escolas.nome,' (',municipios.nome,')') AS nome"), 'escolas.id');

        if($municipio_id){
            $list = $list->where('escolas.municipio_id','=',$municipio_id);
        }

        $list = $list->lists('nome', 'id');

        $arr = $list->toArray();
        $arr = ['' => 'Selecione'] + $arr;




        return $arr;
    }

    public function selectEscolasDaRegional($regional_id)
    {

        $municipiosDaRegional = Municipio::join('regionais','regionais.id','=','municipios.regional_id')
                              ->where('municipios.regional_id','=',$regional_id)
                              ->select('municipios.id')
                              ->lists('id');

        $escolas = Escola::whereIn('escolas.municipio_id',$municipiosDaRegional);

        return $escolas;
    }

    public function selectEscolasPeloIdDaRegional($regional_id)
    {

        $municipiosDaRegional = Municipio::join('regionais','regionais.id','=','municipios.regional_id')
            ->where('municipios.regional_id','=',$regional_id)
            ->select('municipios.id')
            ->lists('id');

        $escolas = Escola::whereIn('escolas.municipio_id',$municipiosDaRegional)
            ->select('escolas.nome','escolas.id')
            ->lists('nome','id')
            ->toArray();

        return $escolas;
    }


    public function selectByMunicipio($municipio_id){

            $escolas = Escola::whereIn('escolas.municipio_id',$municipio_id)
            ->select('escolas.nome','escolas.id')
            ->lists('nome','id')
            ->toArray();


        return $escolas;

    }

    //////////Métodos para Acompanhamento Geral//////////////////////////

    public function escolasRede($fase_id, $rede_id, $municipio_id, $regional_id){


        $escolas = $this->join('equipes','equipes.escola_id','=','escolas.id')
                        ->join('municipios','municipios.id','=','escolas.municipio_id')
                        ->where('equipes.fase_id','=',$fase_id);

        if($rede_id) {
            $escolas = $escolas->where('escolas.rede_id','=',$rede_id);
        }

        if($municipio_id) {
            $escolas = $escolas->where('escolas.municipio_id','=',$municipio_id);
        }

        if($regional_id) {
            $escolas = $escolas->where('municipios.regional_id','=',$regional_id);
        }

        $escolas = $escolas->select('equipes.escola_id')
                           ->distinct()
                           ->get();

        return $escolas;
    }
}

model Cadastrorequest

   <?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CadastroRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
               'cnpj' => 'unique:escolas,cnpj',
               'razao_social' => 'required',
               'nome' => 'required',
               'inep' => 'required|unique:escolas,inep',
               'rede_id' => 'required',
               'logradouro' => 'required',
               'numero' => 'required',
               'bairro' => 'required',
               'municipio_id' => 'required',
               'cep' => 'required',
               'telefone' => 'required',
               'representante_cpf' => 'required|cpf|unique:escolas,representante_cpf',
               'representante_nome' => 'required',
               'representante_funcao' => 'required',
               'representante_telefone' => 'required',
               'representante_email' => 'required|unique:escolas,representante_email',
               'senha' => 'required|confirmed',
        ];
    }
}
  • would have like you to post your model Usuario and Escola?, along with this CadastroRequest?

  • By submitting the Librarian he accuses some error?

  • no error it just appears the form again without any error

  • you can give a dd() in the model of usuário and see if the data is being sent?

  • is not returning dd() in model. only in authcontroller getcadastro but returns empty

  • if in the controller the dd is returning null is a sign that your controller is failing to catch the values of form

Show 1 more comment

1 answer

0

Your form has none url to be directed, put this in your form so that it is directed to your controller and he will be able to collect the data.

{{ Form::open(['url' => 'sua-rota-aqui']) }}
        <input type="hidden" name="_method" value="POST">
{{ Form::close() }}

Browser other questions tagged

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