Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

The programs() method will create an attribute that will be a Collection of the relationships, so you access them via:

$developers->programs
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Can you show us your database tables ?

Last updated 1 year ago.
0

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'.

Last updated 1 year ago.
0

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) ?

Last updated 1 year ago.
0
$developers->each(); //Execute a callback over each item.

try a foreach

foreach($developer as $developer) {
    var_dump($developer->programs); // shortcut for $developer->programs()->get();
}
Last updated 1 year ago.
0

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()

Last updated 1 year ago.
0

typo, $developers

Last updated 1 year ago.
0

zenry said:

typo, $developers

var_dump($developer->programs) returns string(2) "13" Its a simple integer value

Last updated 1 year ago.
0

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...
   }
}
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ckissi ckissi Joined 9 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.