Dependent register taking ID of the respective employee with laravel 5.2

2

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

To be more specific, I already have the field where you display the list of dependents for a particular employee. I would just like to be able to create the new dependent by taking his id in the field.

Here are the codes:

The view of the dependency creation form:

    @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 dependent controller:

   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 have tested several things, one of them is, in the view, to change the value null (in the input of the ID) by an employee variable taking its ID, but always gives variable error undeclared.     

asked by anonymous 06.10.2016 / 18:05

2 answers

3

I have an idea of what could be done. I just can not test here on my machine.

Pass the employee id on the route of the new form:

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

Modify the newForm action on 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 official_id as hidden in your view:

{!! Form::hidden('funcionario_id', isset($funcionario_id)?$funcionario_id:0) !!}
    
06.10.2016 / 18:42
1

The problem was that the variable was being instantiated erroneously. Thanks for the help!

    
13.10.2016 / 19:46