Recover database record and display on user screen with Laravel 8

Asked

Viewed 34 times

-1

I’m a beginner and I’m struggling to get back the database data and display on screen. I have the following scenario: inserir a descrição da imagem aqui

I have an input field to put the numbers of a CPF that has already registered in the database(MY GOAL IS TO FIND THIS INFORMATION AND DISPLAY IT ONLY ON THE SCREEN).

As shown in the image, all database records are returning on the screen and I need to know how to fix this.

By clicking the Query button, it should return me the record based on the informed Cpf.

I’m using Laravel 8, Mysql database.

Follow my code for comparison: inserir a descrição da imagem aqui

Route::get('/test', [Matriculacontroller::class, 'index']);

Route::post('/create', [Matriculacontroller::class, 'create']);

inserir a descrição da imagem aqui

<div style="display: block; margin-bottom: 0;">

        <div class="btn-consulta">
            <form method="GET" action="/teste" >
                @csrf

                <table style="margin-bottom: 0;">
                    <tr>
                        <td>
                            CPF: <input type="text" name="cpf_consulta" size="10" placeholder="só numeros"/>
                                
                            <button type="submit" class="btn btn-info">Consultar</button>
                                
                        </td>
                    </tr>
                </table>


            </form>
        </div>

inserir a descrição da imagem aqui

<div style="display: flex">
                
                <table class="table table-primary table-hover">                   
                    
                    <thead class="thead-primary">

                        <th> ID </th>                
                        <th> Nome </th>
                        <th> Data de Nascimento</th>
                        <th> RG </th>
                        <th> CPF </th>
                        <th> Sexo </th>                        

                    </thead>

                    <tbody>

                        @foreach ($dados as $dado)

                            <tr>
                                <td> {{ $dado->id }} </td>
                                <td> {{ $dado->nome_completo }} </td>
                                <td> {{ $dado->data_nascimento }} </td>
                                <td> {{ $dado->rg }} </td>
                                <td> {{ $dado->cpf }} </td>
                                <td> {{ $dado->sexo }} </td>                               
                            </tr>
                            
                        @endforeach
                        
                    </tbody>                       

                </table>
            </div>
        </div>

inserir a descrição da imagem aqui

$alunos = DB::table('inscricaos')
                      ->join('areas', 'inscricaos.area_id', '=', 'areas.id')
                      ->join('planos', 'inscricaos.plano_id', '=', 'planos.id')
                      ->join('carreiras', 'inscricaos.carreira_id', '=', 'carreiras.id')
                      ->select('inscricaos.id', 'inscricaos.nome_completo', 'inscricaos.cpf', 
                               'areas.nome as area', 'planos.tipo', 'planos.desc', 'carreiras.nome as carreira')
                      //->where('inscricaos.id', '=', $id)                      
                      ->get();

$dados = DB::table('inscricaos')
          ->select('inscricaos.id', 'inscricaos.nome_completo',      
                   'inscricaos.data_nascimento',                   
                   'inscricaos.rg','inscricaos.cpf',inscricaos.sexo')                                                           
          ->get();


        $array = array(
            'alunos' => $alunos,
            'dados' => $dados,
        );
      
        return view('/teste', $array);

I did some research and found that there is a way to make an AJAX request to get this data, but as a beginner I am having a lot of difficulty to solve this issue.

NOTE: In the Controller screen print, there is a part with the variable $students and an innerJoin, in this case this part is OK, it is the variable $data down that is the problem.

  • 1

    @Danizavtz thanks for the tip, I edited the post by inserting the codes in text.

No answers

Browser other questions tagged

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