Relate more than one value of the same row to another table (Laravel)

Asked

Viewed 449 times

0

I’m new to Laravel and wanted to know if you can solve my problem without being in pure PHP, that is, using Laravel’s own resources. Follow the current situation and what I wish to do:

I have a table called Departments, where I include the department name (column name) and 1 focal point (column pontofocal_id), 1 manager (column gestor_id) and 1 or more backups (column backup). By saving the record I save the Ids of the pontofocal, backups and managers I pick from the Table users. Until then, I use in my App/Department file the hasOne for the users table and I can pull the user name by the ID inserted in the Departments table.

public function pontofocal(){
    return $this->hasOne(Usuario::Class, 'id', 'pontofocal_id');
}

public function gestor(){
    return $this->hasOne(Usuario::Class, 'id', 'gestor_id');
}

public function backups(){
    return $this->hasOne(Usuario::Class, 'id', 'backup');
}

But the Backup column I can save more than one ID in the same record. I separate them by comma.

In my php list.blade.I used a foreach inside the other foreach to take the value of the backup field (example: 1,4,5) to transform into an array containing IDS 1, 4 and 5, using a explode and returns this information for the $departameno-variable>backup, so you can use the relationship and get the name field of the users table as per the code below:

@foreach($departamentos as $departamento)
                    <tr>
                        <td>{{ $departamento->nome }}</td>
                        <td>{{ $departamento->gestor->name }}</td>
                        <td>{{ $departamento->pontofocal->name }}</td>
                        @php
                            $arraybackups = explode(",",$departamento->backup);
                        @endphp
                        <td>
                            @foreach($arraybackups as $arraybackup)
                        @php
                            $departamento->backup = $arraybackup;
                        @endphp
                            {{ $departamento->backups->name }}
                            @endforeach
                        </td>

At first it works but it seems that it takes the name field value only from the first ID and repeats to the others even though they are different Ids. As below:

exemplo1

When giving an echo to see if I am recording the correct Ids extracted from the array, I see that yes, they are correct as below. see q are different Ids, but it always brings me the same name as the first line ID:

exemplo2

Is there any way I can do it?

Thank you!

  • You need to make a junction of these tables... but, by the way the column records all the Ids this is not possible ... rethink the tables and save the information of each id in its own line ... IE, stores everything in the same field is a big mistake !!!

  • Hello Virgilio, thank you for answering but you’re wrong. In some cases this is the best way to speed up the process and decrease the size of the tables. I’ve figured out how to do what I need. For this I used a table Inner Join and in the field I need to search the ids I used FIND_IN_SET() from MYSQL. They do exactly what I needed. Then I grouped the results and used GROUP_CONCAT in the field I needed. All solved without needing to create other tables. Abs

  • you solved your problem with two functions, then I ask what was the cost!? But it’s okay to find a solution that today can help you and with some change can be harmful I prefer to still use a well organized repository ...

1 answer

0

I managed to solve my problem and am posting here how I solved.

I solved using FIND_IN_SET() native MYSQL function. It fetches the field values and crosses with the values of the other table and returns everything in a separate row. That is, it returns me:

Financial Back Up 01

Financial Back Up 02

And so on and so forth. Then after I made a GROUP BY to group by the department name and in the NAME field that brings the backup names I used the function GROUP_CONCAT() mysql function tb.

I hope I’ve helped!

Abs

Browser other questions tagged

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