Relationship (via Eloquent) not getting data on Front End

I followed the write up here - https://statamic.dev/extending/relationship-fieldtypes

And created a custom relationship fieldtype called "Coaches" - I assigned it to my pages collection. On my "Home" page in the pages collection - I assigned a few coaches - which all show up properly in the control panel and the database (using DB instead of flat file)

However when trying to loop over them in the front end - I don't get any additional data about the coach - just an array of ids - looping over it - i can't access any fields in the loop either

Setup: Statamic 3 storing content in Database Installed into existing laravel app

Relevant Data from the Database (which would normally be in a flat file) - associated to this 'page'

      {
         "coaches":[
            41,
            42,
            37,
            38,
            44
         ],
         "type":"coaches",
         "enabled":true,
         "link_to_all_coaches":false,
         "featured_coach_title":"Meet our Coaching Team",
         "container_width":"full"
      },

Front End Template

{{ coaches }} will loop 5 times - which makes sense because I have 5 coaches assigned - but first_name a DB field - will always be blank - if I just output {{ coaches }} instead of looping - I see an array of IDs

{{ coaches }}            
            <div>
                <a href="">
                    <img src="###" alt="Coach Headshot" />
                    <strong>{{ first_name }} - City, ST</strong>
                </a>
            </div>
{{ /coaches }}

Fieldtypes/Coaches.php

class Coaches extends Relationship
{    
    // https://statamic.dev/extending/relationship-fieldtypes
    protected $canCreate = false;
    protected $canSearch = false;

    public function getIndexItems($request)
    {
        //dd(1);
        $byRole = Role::findByName('coach');   
        $coaches = User::orderBy('last_name')->orderBy('first_name')->whereIn('id', $byRole->users->pluck('id')->toArray())->paginate(env('PAGINATION_LIMIT'));

        return $coaches;
    }

    protected function getColumns()
    {
        //dd(2);
        return [
            Column::make('first_name'),
            Column::make('last_name'),
            Column::make('email'),
        ];
    }

    public function getItemData($values, $site = null)
    {
        //dd(3);
        $coaches = User::whereIn('id', $values)->get();
        foreach($coaches as $c)
        {
            $c->title = $c->name;
        }
        return $coaches;
    }

    protected function toItemArray($id, $site = null)
    {
        //dd(4);
        if ($user = User::find($id)) {
            $u = [
                'first_name' => $user->first_name,
                'last_name' => $user->last_name(),
                'id' => $user->handle(),
            ];
            return $u;
        }

        return $this->invalidItemArray($id);
    }

}
Answered by Duncan McClean!
>>>>>>> Answered <<<<<<<
3 Replies
2 Followers