How to display data correctly in a datatable

Asked

Viewed 34 times

0

I need some information about running two tables through the index. I have the following code in the controller:

public function index(){

    $tabela = capitulo::orderby('id', 'desc')->paginate();
    
    $tabela1 = documentacao::orderby('id', 'desc')->paginate();

    return view('gestao-documental.index', ['itens' => $tabela, 'itens1' => $tabela1]);

   
}

And I want to pass the data of the two tables :: Chapter (ID, chapter) :: Documentation(ID, Chapter, Document, version) for the following datatable

<div class="card shadow mb-4">

   <div class="card-body">
      <div class="table-responsive">
         <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
         <thead>
           @foreach($itens as $item)
                <tr>
                    <th> {{$item->capitulo}} </th>                        
                </tr>                      
            </thead>               
            <tbody>
            @foreach($itens1 as $item1)
            <tr>
                <td> {{$item1->documentacao}} </td>                    
            </tr>
            @endforeach     

            </tbody>
            @endforeach  
        </table>
    </div>    
</div>
  • Can you post the relationship code of the eloquent model? What you need is, it seems to me, a loop inside another, but you need to know what the data and relationships are to be effective in helping

  • I changed the question a little. I hope it is a little more complete.

  • But the table and Tabela1, chapters and documentation relate in the bank? If yes and you need, for example, to put cap inside doc or vice versa, you need, in addition to being correct the modeling in the database, to use Relations in your models to be able to recover the related records of a "parent" record ... can add model codes and modeling this part of the bank?

  • They are related (Foreign key). That is, the main table (chapter) is always necessary to create data in the documentation table. is the only link that the tables have is Id_documentacao. I do not know if I could explain well .

  • so you need to add Relationship methods to the models and navigate correctly on Blade, I’ll put an example below

1 answer

1


You need to add Relationship methods to the models and navigate correctly on the Blade.

An example for the Chapter model:

<?php
// imports
class Capitulo extends Model
   // configuração do modelo

   public function documentos() {
       // considerando que a modelagem segue os padrões Laravel, senão é necessário passar os parâmetros de chave estrangeira corretamente
      return $this->hasMany(Documentacao::class);
   }

In your controller


public function index(){

    $capitulos = Capitulo::orderby('id', 'desc')->paginate();
    
    return view('gestao-documental.index', compact('capitulos'));
   
}

In the vision on Blade

<div class="card shadow mb-4">

   <div class="card-body">
      <div class="table-responsive">
         <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
         <thead>
           @foreach($cpaitulos as $cap)
                <tr>
                    <th> {{$cap->capitulo}} </th>                        
                </tr>                      
            </thead>               
            <tbody>
               @foreach($cap->documentos as $doc)
                  <tr>
                     <td> {{$doc->documentacao}} </td>                    
                  </tr>
               @endforeach     
            </tbody>
            @endforeach  
        </table>
    </div>    
</div>

I believe this helps you to direct the solution

Browser other questions tagged

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