Laravel - Controller - Show data from two tables

Asked

Viewed 35 times

-1

I’m having trouble showing data for two related tables. 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]);

   
}

I can see the Chapter but the documentation does not. I may be wrong for the Return view:

<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>
            @foreach($itens1 as $item1)
            <tbody>
            
            <tr>
                <td> {{$item1->documentacao}} </td>
     
                
            </tr>
            @endforeach     

            </tbody>
            @endforeach  
        </table>
    </div>    
</div>

1 answer

0

First of all it would be interesting to know what error is occurring, but you can already notice that your foreach are not well structured. For example you started a foreach below the thead tag and the closing of the thead is inside the foreach, that is, it will iterate as many times as this loop exists, creating several closing thead tag where there is no opening one. Another thing, I don’t know what your goal is, but the documentation part will be iterated every time you pass the chapter loop, for example, when you loop the first chapter, it will take all the registered lines of the documentation and iterate, and this will make the view repeatable, maybe if your intention is to show the table documentation data that is related to a linked chapter, you should use the functions that you put in Modal to search for this data. Example:

CapituloModel.php

public function documentacoes() {
     return $this->hasMany(DocumentacaoModel::class)
}

and in the view

@foreach($itens->documentacoes as $documentacao)
            <tr>
                <th> {{$documentacao->item_da_documentacao}} </th>
            </tr>
@endforeach

I do not know if I helped, but I advise you to structure the HTML part, and explain what error you are having.

Browser other questions tagged

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