简体   繁体   中英

How to display specific data of an SQL table using php blade

I have a program where an user is assigned to a project which has images and texts.

And obviously, in said program, an User can see the projects assigned to him and, by extension, the texts and images of the project.

But I have problem, where, if one user has 2 projects, when he looks for one project, he sees the data of both of them, as you can see in the next image:

例子

As you can see, it's showing both text 1 and 2 which are "Cambio 3" and "Cambio 1" respectively, but, it should only show the first text. The code I have for this is:

index_proyecto_blade.php

<div class="row pt-4">
                    <h3>Textos</h3>

                    @foreach($usuario_proyecto as $usr_prt)

                    @foreach($proyecto_data as $prt)

                    @foreach($texto_data as $txt)

                    @foreach($texto_proyecto_data as $txt_prt)

                        @if($txt_prt->id_texto_data == $txt->id)

                        @if($txt_prt->id_proyecto_data == $prt->id_proyecto)

                        @if($usr_prt->id_proyecto == $prt->id_proyecto)

                            Id de texto: {{ $txt->id }}
                            <br>
                            <div class="col-2">
                                <h5>{{ $txt->caption }}</h5>
                                <p>{{ $txt->texto }}</p>
                            </div>
                            <br>
                        @endif
                        @endif
                        @endif
                    @endforeach
                    @endforeach
                    @endforeach
                    @endforeach
                </div>

And here are the tables in play:

Usuario_Proyecto (relation between user and a project)

Usuario_Proyecto::all()
=> Illuminate\Database\Eloquent\Collection {#3061
     all: [
       App\Usuario_Proyecto {#3069
         id: 1,
         id_user: 2,
         id_proyecto: 1,
         created_at: null,
         updated_at: null,
       },
       App\Usuario_Proyecto {#3062
         id: 2,
         id_user: 2,
         id_proyecto: 2,
         created_at: null,
         updated_at: null,
       },
     ],
   }

Proyecto_data (clone of an original project)

Proyecto_Data::all()
=> Illuminate\Database\Eloquent\Collection {#3060
     all: [
       App\Proyecto_Data {#3063
         id: 1,
         nombre_proyecto: "["proyecto_test"]",
         id_proyecto: 1,
         created_at: null,
         updated_at: null,
       },
       App\Proyecto_Data {#3058
         id: 2,
         nombre_proyecto: "["proyecto_test_2"]",
         id_proyecto: 2,
         created_at: null,
         updated_at: null,
       },
     ],
   }

Texto_Data (clone of a text associated to a project/text associatod to Proyecto_Data)

texto_data::all()
[!] Aliasing 'texto_data' to 'App\texto_data' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#3068
     all: [
       App\texto_data {#3069
         id: 1,
         nombre: "["this is a test caption"]",
         texto: "["Cambio 3"]",
         min: 1,
         max: 100,
         created_at: null,
         updated_at: null,
       },
       App\texto_data {#3070
         id: 2,
         nombre: "["palaBRA2"]",
         texto: "cambio 1",
         min: 1,
         max: 100,
         created_at: null,
         updated_at: null,
       },
     ],
   }

Texto_Proyecto_Data (Relation between texto_data and proyecto_data)

texto_proyecto_data::all()
[!] Aliasing 'texto_proyecto_data' to 'App\texto_proyecto_data' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#3071
     all: [
       App\texto_proyecto_data {#3050
         id: 1,
         id_texto_data: 1,
         id_proyecto_data: 1,
         created_at: null,
         updated_at: null,
       },
       App\texto_proyecto_data {#3045
         id: 2,
         id_texto_data: 2,
         id_proyecto_data: 2,
         created_at: null,
         updated_at: null,
       },
     ],
   }

The reason of why I use clones is because the user can edit their version of a project.

I make sure (I think at least) that the program only shows the data if the proyecto_id of text is the same the one in proyect and user. But it still shows the ones the text of other projects.

Why is that happening? I'm doing something wrong?

the file name isn't right.

index.blade.php

In your controller, return view like return view('proyecto.index'); ; Basically folder.file

Always name the files xxx.blade.php

Hope it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM