I am going to reformulate the question because I think I have not oriented it as I should.
Currently I have two tables in database TypeFactors , Factors with relation 1:N
.
To obtain the data of these tables, I perform the following query:
$factors = DB::table('type_factors')
->join('factors', 'type_factors.id', '=', 'factors.typefactor_id')
->select('type_factors.typename', 'factors.typefactor_id', 'factors.factorname')
->get();
And I get a result of the type:
What I would like now is to be able to group the obtained data in a multidimensional array before passing it to the view; of the type :
Example:
[
"Conocimiento y Aptitudes" => [
// Aquí todos los resultados, con el mismo typename "Conocimiento y Aptitudes"
],
"Responsabilidad" => [
// Aquí todos los resultados, con el mismo typename "Responsabilidad"
],
// ...
]
Thanks in advance.
All the best.
Well, a few hours ago I answered a question from another community colleague who needed relatively the same thing, but in javascript instead of PHP . I have adapted that solution to PHP and it is as follows:
With a
foreach
we go through and check, if the current group exists, if not we create it and add the item like this:This should work just fine for you, grouping as you like by
typename
However, I will give you a piece of advice, I think you should consider and see the use of the groupBy closure in Laravel and in SQL in general.
Also remember that you have in your hands, along with Laravel , Eloquent and its relationships.
Using Laravel and not taking advantage of its features is like grinding wheat while having flour in your pantry haha. But it's just advice , you use of course, what you think is best and what you need
Cheers :)