Register of dependent picking ID of the respective employee with Laravel 5.2

Asked

Viewed 498 times

2

My problem is that when registering a new dependent the input, which has the employee ID that the dependent will be related to, is automatically filled with the ID of the given user and not that the person who is registering has to enter the employee id.

To be more specific, I already have the field where displays the list of dependents of a particular employee. I just wish I could create the new dependent by already getting his id in the field.

Follow below the codes:

The dependent creation form view:

    @extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Dependentes
                    <a class="pull-right" href="{{url('dependentes
                                                ')}}">Lista de Dependentes</a>
                </div>

                <div class="panel-body">

                        @if(count($errors) > 0)
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach($errors->all() as $error)
                                        <li> {{$error}} </li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif

                        @if(Session::has("msg_sucesso"))
                            <div class="alert alert-success"> {{ Session::get('msg_sucesso')}} </div>

                        @elseif(Session::has('msg_erro'))
                            <div class="alert alert-danger"> {{Session::get('msg_erro')}} </div>

                        @endif


                        @if(Request::is('*/editar'))
                            {!! Form::model($dependente, ['method' => 'PATCH', 'url' => 'dependentes/'.$dependente->id]) !!}
                        @else
                            {!! Form::open(['url' => 'dependentes/salvarDpt']) !!}
                        @endif 

                        {!! Form::label('funcionario_id', 'ID: ') !!}
                        {!! Form::input('text', 'funcionario_id', null, ['class' => 'form-control', 'autofocus']) !!}
                        <br>
                        {!! Form::label('nome', 'Nome: ') !!}
                        {!! Form::input('text', 'nome', null, ['class' => 'form-control', 'autofocus', 'placeholder' => 'Nome']) !!}   
                        <br>
                        {!! Form::label('dataNascimento', 'Data: ') !!}
                        {!! Form::input('date', 'dataNascimento', null, ['class' => 'form-control', 'autofocus', 'placeholder' => 'AAAA-MM-DD']) !!}
                        <br>
                        {!! Form::submit('Salvar', ['class' => 'btn btn-primary']) !!}

                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

The controller of the dependent:

   namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Requests\ValidarDepRequest;
use App\Dependente;

use Redirect;

use App\Funcionario;

class DependentesController extends Controller{

    public function index(){

        $dependentes = Dependente::get();
        return view('dependentes\lista', ['dependentes' => $dependentes]);

    }

    public function novoForm(){

        return view('dependentes.formDpt' );
    }

    public function salvar(ValidarDepRequest $request){

        $dependente = new Dependente();

        try{

            $dependente->create($request->all());

            \Session::flash('msg_sucesso', 'Dependente cadastrado!');

        } catch(\Illuminate\Database\QueryException $e){

            var_dump($e->errorInfo);

            \Session::flash('msg_erro', 'ID do funcionário não existe!');

        }

        return Redirect::to('dependentes/formDpt');
    }

    public function editar($id){

        $dependente = Dependente::findOrFail($id);

        return view('dependentes.formDpt', ['dependente' => $dependente]);
    }

    public function atualizar($id, ValidarDepRequest $request){

        $dependente = Dependente::findOrFail($id);
        $dependente -> update($request->all());

        \Session::flash('msg_sucesso', 'Funcionário atualizado!');

        return Redirect::to('dependentes/'.$dependente->id.'/editarDpt');
    }

    public function deletar($id){

        $dependente = Dependente::findOrFail($id);
        $dependente -> delete();

        \Session::flash('msg_sucesso', 'Dependente deletado!');

        return Redirect::to('dependentes');

    }


}

And the routes:

   Route::get('/', function () {
    return view('welcome');
});

Route::auth();

Route::get('home', 'HomeController@index');

Route::get('funcionarios', 'FuncionariosController@index');
Route::get('funcionarios/novoForm', 'FuncionariosController@novoForm');
Route::get('funcionarios/{funcionario}/editar', 'FuncionariosController@editar');
Route::post('funcionarios/salvarFuncionario', 'FuncionariosController@salvarFuncionario');
Route::patch('funcionarios/{funcionario}', 'FuncionariosController@atualizar');
Route::delete('funcionarios/{funcionario}', 'FuncionariosController@deletar');


Route::get('dependentes', 'DependentesController@index');
Route::get('dependentes/formDpt', 'DependentesController@novoForm');
Route::post('dependentes/salvarDpt', 'DependentesController@salvar');
Route::delete('dependentes/{dependente}', 'DependentesController@deletar');
Route::get('dependentes/{dependente}/editarDpt', 'DependentesController@editar');
Route::patch('dependentes/{dependente}', 'DependentesController@atualizar');


Route::get('funcionarios/{funcionario}/listarDpt', 'FuncionariosController@listarDpt');

Route::get('funcionarios/{funcionario}/listarDpt', 'FuncionariosController@listarDpt');


Route::get('projetos', 'ProjetosController@index');
Route::get('projetos/novoForm', 'ProjetosController@novoForm');
Route::post('projetos/salvarProjeto', 'ProjetosController@salvarProjeto');
Route::get('projeto/projetos/edit', 'ProjetosController@edit');

I tested several things, one of them is, in the view, to exchange the null value (in the ID input) for an employee variable by picking its ID, but always gives undeclared variable error.

2 answers

3

I have an idea of what could be done. I just have no way to test here on my machine.

Pass employee id on the new form route:

Route::get('dependentes/formDpt/{id?}', 'DependentesController@novoForm');

Modify the new action in the dependency controller to receive the employee id:

public function novoForm($id = null){
    $funcionario_id = ($id != null)?$id:0;

    return view('dependentes.formDpt', compact('funcionario_id') );
}

Finally put the functio_id as Hidden in your view:

{!! Form::hidden('funcionario_id', isset($funcionario_id)?$funcionario_id:0) !!}
  • Thanks for the reply. Unfortunately it didn’t work.

  • What was the mistake that came?

1

The problem was the variable that was being instantiated erroneously. I thank you for your help!

Browser other questions tagged

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