The programs() method will create an attribute that will be a Collection of the relationships, so you access them via:
$developers->programs
thevelement said:
The programs() method will create an attribute that will be a Collection of the relationships, so you access them via:
$developers->programs
Unfortunately I got this: Undefined property: Illuminate\Database\Eloquent\Collection::$programs
So you want to only grab the programs?
You can't grab 'programs' from a collection.
You could filter through every developer returned, and then build your own array containing only the 'programs'.
I'm able to go over developers by :
$developers->each(function($developer) {
echo "<pre>"; var_dump($developer);echo "</pre>";
});
But cannot get collection of programs for each developer. I mean:
$developers->each(function($developer) {
echo "<pre>"; var_dump($developer->programs);echo "</pre>";
});
The code above returns just a number, It seems to be the count of programs for specific developer
So the code above shows this for me:
--string(2) "63"
--string(1) "0"
--string(1) "0"
--string(1) "0"
--string(1) "0"
--string(1) "1"
--string(1) "0"
--string(1) "9"
--string(1) "1"
--string(1) "0"
--string(1) "0"
--string(1) "1"
--string(1) "1"
--string(1) "1"
--string(1) "1"
--string(1) "1"
--string(1) "1"
--string(1) "0"
--string(1) "1"
--string(1) "1"
--string(1) "1"
--string(1) "1"
Here is a var_dump of first $developer collection with 63 programs in programs collection (line 101) in it: http://laravel.io/bin/z6LeX
So now the question is ... How to access that programs collection (line 101) ?
$developers->each(); //Execute a callback over each item.
try a foreach
foreach($developer as $developer) {
var_dump($developer->programs); // shortcut for $developer->programs()->get();
}
zenry said:
$developers->each(); //Execute a callback over each item.
try a
foreach
foreach($developer as $developer) { var_dump($developer->programs); // shortcut for $developer->programs()->get(); }
ERROR: Invalid argument supplied for foreach()
zenry said:
typo, $developers
var_dump($developer->programs) returns string(2) "13" Its a simple integer value
ckissi said:
thevelement said:
The programs() method will create an attribute that will be a Collection of the relationships, so you access them via:
$developers->programs
Unfortunately I got this: Undefined property: Illuminate\Database\Eloquent\Collection::$programs
My bad, I got ahead of myself. As others have stated:
foreach ($developers as $developer)
{
foreach ($developer->programs as $program)
{
// Do something...
}
}
My bad, I got ahead of myself. As others have stated:
foreach ($developers as $developer) { foreach ($developer->programs as $program) { // Do something... } }
Invalid argument supplied for foreach()
var_dump($developer->programs) shows string(2) "13" as I said above, when I var_dump the $developers collection I can see programs collection inside http://laravel.io/bin/z6LeX . But when I try to access this collection with foreach it doesnt work and var_dump shows integer value instead of object or array
ckissi said:
My bad, I got ahead of myself. As others have stated:
foreach ($developers as $developer) { foreach ($developer->programs as $program) { // Do something... } }
Invalid argument supplied for foreach()
var_dump($developer->programs) shows string(2) "13" as I said above,
When I var_dump the $developers collection I can see programs collection inside http://laravel.io/bin/z6LeX .
But when I try to access this collection with foreach it doesnt work and var_dump shows integer value instead of object or array
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community