How to make a filter in Laravel 5.3

Asked

Viewed 70 times

0

Hello, I’ve been trying to make a form that receives a Cpf and if this Cpf is in the database it returns all the data linked to this Cpf (name, position, code etc) right next to the form, in the same view. But I’m having a lot of trouble doing it. Below are the codes:

Controller:

class SolicitanteController extends Controller
{   
    #Tela de Inicio
    public  function inicio(){

        $resultado = Servidor::all();
        return view('usuarios/solicitantes/inicio');

    }

    public function pegarServidor(Request $request){

        $resultado = Servidor::all();
        if($request->has('cpf')){

            $resultado = $request->get('cpf');

            return view('usuarios/solicitantes/inicio', ['resultado' => $resultado]);
        }


        return Redirect::to('usuarios/solicitantes/historico');

    }

View:

@extends('layouts.app')

@section('content')

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

    <div class="container">
        <div class="container">
            <div class="row">
                <div class="col-md-4">
                    <section id="secao_servidor">
                        {!! Form::open(['method'=>'GET', 'url' => 'usuarios/solicitantes/pegarServidor']) !!}

                            {!! Form::label('cpf', 'CPF: ') !!}
                            {!! Form::input('text', 'cpf', null, ['class' => 'form-control', 'autofocus']) !!}
                            <br>
                            {!! Form::submit('Confirmar', ['class' => 'btn btn-primary']) !!}

                        {!! Form::close() !!}
                    </section>  
                </div>
                <div class="col-md-8">
                    <section id="secao_dadosServidor">
                        {!! Form::model('$resultado', ['method'=>'PATH', 'url' => 'usuarios/solicitantes/adicionarServidor]) !!}
                        <div class="col-md-4">
                            <ul>

                                <li>{!! Form::input('text', 'nome',$resultado->nome, ['class' => 'form-control', 'autofocus']) !!} </li>

                                <li>Função: </li>
                                <li>Nº Banco:</li>
                            </ul>
                        </div>
                        <div class="col-md-4">
                            <ul>
                                <li>Cargo: </li>
                                <li>Beneficiario: </li>
                                <li>Agência: </li>
                                <li>Conta: </li>
                            </ul>
                        </div>
                        <div>
                        {!! Form::close() !!}
                        </div>

                    </section>
                </div>
                </div>
            </div>
        </div>
    </div>  

Here in the view I made two forms, one for each take Cpf and the other to display the fields. I put only one input to test. I want to load the data automatically into inputs.

  • you do a CPF search and want it to appear in the form below?

  • That’s right. Showing all user data that has this Cpf.

  • Servidor::all() is where the data is?

  • Yes. I believe you have used kkk correctly

  • Not exactly, if that’s the layer that brings the CPF data you’re doing wrong is inside the If makes a Where and climbs the values up!

  • Apologies, but when you say "move up the dice," which means?

  • You are learning Laravel?

  • Yes. I’ve been with him for a few weeks.

Show 4 more comments
No answers

Browser other questions tagged

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