Many Relationship for Many Laravel - For each Blade

Asked

Viewed 79 times

0

I’m starting at the Laravel. I have the following problem. I have three tables with relationship N:N.

  • ctrl_cm_tecnicos
  • ctrl_lista_tecnicos
  • acl_lista_tecnicos

The tables relate as follows. inserir a descrição da imagem aqui

I have a Controller Named Technical Listingscontroller, which I am sending the data to my index. I have tested the relationship between ctrl_lista_tecnicos and ctrl_cm_tecnicos through dd($this->objTecnico->find(664)->Relcms), and the relationship occurs perfectly.

    class ListagemTecnicosController extends Controller
{
    private $objTecnico;
    private $objCm;
    private $objAcl;

    public function __construct()
    {
        $this->objTecnico = new CtrlListaTecnico();
        $this->objCm = new CtrlCmTecnico();
        $this->objAcl = new AclCmTecnico();
        
    }   

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        

        //dd($this->objCm->find(62)->RelTecnicos);
        //dd($this->objTecnico->find(664)->RelCms);

        $tecnicos=$this->objTecnico->all();
        $cm=$this->objCm->all();
        return view('index', compact('tecnicos', 'cm'));

    }

However when I arrive at my index, I do not know how to display Regional which is in the table ctrl_cm_tecnicos.

 <div class="col-12 m-auto">         
          <table class="table">
            <thead class="thead-dark">
              <tr>
                <th scope="col">Id</th>
                <th scope="col">Nome</th>
                <th scope="col">Rg</th>
                <th scope="col">CPF</th>
                <th scope="col">Regional</th>
              </tr>
            </thead>
            <tbody>
                
                @foreach ($tecnicos as $t)
                        
                        <tr>
                            <th scope="row">{{$t['id']}}</th>
                            <td>{{$t['nome']}}</td>
                            <td>{{$t['rg']}}</td>
                            <td>{{$t['cpf']}}</td>
                            <td>{{'Regional'}}</td>
                            
                        </tr>
                @endforeach
              
            </tbody>
          </table>
    </div>

The display of the other data is correct, as screen below, missing only the regional that is the cm of the table -> ctrl_cm_tecnicos.

Anyone have any ideas ? I’ve been searching for a solution for over a day but haven’t found it.

inserir a descrição da imagem aqui

  • but who is regional in their table?

1 answer

0


I changed the approach and created a query in the controler through the method DB::table Listtechniquescontroller to display the data I would like that is the field cm of the table ctrl_cm_tecnicos.cm. I don’t know if it was the right way to go, but it worked.

 $tecnicos = DB::table('ctrl_lista_tecnicos')
        ->join('acl_cm_tecnicos', 'acl_cm_tecnicos.id_tecnicos', '=', 'ctrl_lista_tecnicos.id')
        ->join('ctrl_cm_tecnicos', 'ctrl_cm_tecnicos.id', '=', 'acl_cm_tecnicos.id_cms')
        ->select('ctrl_lista_tecnicos.*', 'ctrl_cm_tecnicos.cm')
        ->get();
   


    return view('index', ['tecnicos' => $tecnicos]);

At index.Lade it looked like this.

 @foreach ($tecnicos as $t)
                    
                    <tr>
                        <th scope="row">{{$t->id}}</th>
                        <td>{{$t->nome}}</td>
                        <td>{{$t->rg}}</td>
                        <td>{{$t->cpf}}</td>
                        <td>{{$t->cm}}</td>
                    </tr>
            @endforeach

And finally I was able to display the cm field of the table ctrl_cm_tecnicos.cm .

inserir a descrição da imagem aqui

  • You can mark use own answer as the correct one, for anyone navigating this question understand that this is a solution to the problem

Browser other questions tagged

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