0
votes

I have two models: Account:

<?php

namespace App\Models;

class Account extends \Illuminate\Database\Eloquent\Model
{
    public function user()
    {
        return $this->belongsTo('\App\Models\User');
    }
}

..and User:

<?php

namespace App\Models;

class User extends \Illuminate\Database\Eloquent\Model
{
    public function accounts()
    {
        return $this->hasMany('\App\Models\Account');
    }
}

Now, when I do the following - var_dump(\App\Models\User::find(1)->accounts()); - I get back an Illuminate\Database\Eloquent\Relations\HasMany instance but it doesn't contain my accounts for that user's ID. I can confirm that my accounts table does have several rows where the user_id is set to 1. Is there anything I'm doing wrong? I've followed the documentation and I can't see anything different.

Below is the output of var_dump(\App\Models\User::find(1)->accounts()); if it is useful:

object(Illuminate\Database\Eloquent\Relations\HasMany)[67]   protected 'foreignKey' => string 'accounts.user_id' (length=16)   protected 'localKey' => string 'id' (length=2)   protected 'query' => 
    object(Illuminate\Database\Eloquent\Builder)[71]
      protected 'query' => 
        object(Illuminate\Database\Query\Builder)[72]
          protected 'connection' => 
            object(Illuminate\Database\MySqlConnection)[59]
              ...
          protected 'grammar' => 
            object(Illuminate\Database\Query\Grammars\MySqlGrammar)[61]
              ...
          protected 'processor' => 
            object(Illuminate\Database\Query\Processors\MySqlProcessor)[62]
              ...
          protected 'bindings' => 
            array (size=5)
              ...
          public 'aggregate' => null
          public 'columns' => null
          public 'distinct' => boolean false
          public 'from' => string 'accounts' (length=8)
          public 'joins' => null
          public 'wheres' => 
            array (size=2)
              ...
          public 'groups' => null
          public 'havings' => null
          public 'orders' => null
          public 'limit' => null
          public 'offset' => null
          public 'unions' => null
          public 'lock' => null
          protected 'backups' => 
            array (size=0)
              ...
          protected 'cacheKey' => null
          protected 'cacheMinutes' => null
          protected 'cacheTags' => null
          protected 'cacheDriver' => null
          protected 'operators' => 
            array (size=19)
              ...
      protected 'model' => 
        object(App\Models\Account)[57]
          protected 'fillable' => 
            array (size=3)
              ...
          protected 'connection' => null
          protected 'table' => null
          protected 'primaryKey' => string 'id' (length=2)
          protected 'perPage' => int 15
          public 'incrementing' => boolean true
          public 'timestamps' => boolean true
          protected 'attributes' => 
            array (size=0)
              ...
          protected 'original' => 
            array (size=0)
              ...
          protected 'relations' => 
            array (size=0)
              ...
          protected 'hidden' => 
            array (size=0)
              ...
          protected 'visible' => 
            array (size=0)
              ...
          protected 'appends' => 
            array (size=0)
              ...
          protected 'guarded' => 
            array (size=1)
              ...
          protected 'dates' => 
            array (size=0)
              ...
          protected 'touches' => 
            array (size=0)
              ...
          protected 'observables' => 
            array (size=0)
              ...
          protected 'with' => 
            array (size=0)
              ...
          protected 'morphClass' => null
          public 'exists' => boolean false
          protected 'forceDeleting' => boolean false
      protected 'eagerLoad' => 
        array (size=0)
          empty
      protected 'macros' => 
        array (size=4)
          'forceDelete' => 
            object(Closure)[70]
              ...
          'restore' => 
            object(Closure)[69]
              ...
          'withTrashed' => 
            object(Closure)[68]
              ...
          'onlyTrashed' => 
            object(Closure)[66]
              ...
      protected 'onDelete' => 
        object(Closure)[74]
      protected 'passthru' => 
        array (size=12)
          0 => string 'toSql' (length=5)
          1 => string 'lists' (length=5)
          2 => string 'insert' (length=6)
          3 => string 'insertGetId' (length=11)
          4 => string 'pluck' (length=5)
          5 => string 'count' (length=5)
          6 => string 'min' (length=3)
          7 => string 'max' (length=3)
          8 => string 'avg' (length=3)
          9 => string 'sum' (length=3)
          10 => string 'exists' (length=6)
          11 => string 'getBindings' (length=11)   protected 'parent' => 
    object(App\Models\User)[73]
      protected 'fillable' => 
        array (size=5)
          0 => string 'name' (length=4)
          1 => string 'username' (length=8)
          2 => string 'email' (length=5)
          3 => string 'password' (length=8)
          4 => string 'salt' (length=4)
      protected 'connection' => null
      protected 'table' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=9)
          'id' => string '1' (length=1)
          'name' => string 'Martyn' (length=6)
          'username' => string 'martyn' (length=6)
          'email' => string '[email protected]' (length=18)
          'password' => string 'password' (length=8)
          'salt' => string 'salt' (length=4)
          'created_at' => string '0000-00-00 00:00:00' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
          'deleted_at' => null
      protected 'original' => 
        array (size=9)
          'id' => string '1' (length=1)
          'name' => string 'Martyn' (length=6)
          'username' => string 'martyn' (length=6)
          'email' => string '[email protected]' (length=18)
          'password' => string 'password' (length=8)
          'salt' => string 'salt' (length=4)
          'created_at' => string '0000-00-00 00:00:00' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
          'deleted_at' => null
      protected 'relations' => 
        array (size=0)
          empty
      protected 'hidden' => 
        array (size=0)
          empty
      protected 'visible' => 
        array (size=0)
          empty
      protected 'appends' => 
        array (size=0)
          empty
      protected 'guarded' => 
        array (size=1)
          0 => string '*' (length=1)
      protected 'dates' => 
        array (size=0)
          empty
      protected 'touches' => 
        array (size=0)
          empty
      protected 'observables' => 
        array (size=0)
          empty
      protected 'with' => 
        array (size=0)
          empty
      protected 'morphClass' => null
      public 'exists' => boolean true
      protected 'forceDeleting' => boolean false   protected 'related' => 
    object(App\Models\Account)[57]
      protected 'fillable' => 
        array (size=3)
          0 => string 'name' (length=4)
          1 => string 'amount' (length=6)
          2 => string 'user_id' (length=7)
      protected 'connection' => null
      protected 'table' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=0)
          empty
      protected 'original' => 
        array (size=0)
          empty
      protected 'relations' => 
        array (size=0)
          empty
      protected 'hidden' => 
        array (size=0)
          empty
      protected 'visible' => 
        array (size=0)
          empty
      protected 'appends' => 
        array (size=0)
          empty
      protected 'guarded' => 
        array (size=1)
          0 => string '*' (length=1)
      protected 'dates' => 
        array (size=0)
          empty
      protected 'touches' => 
        array (size=0)
          empty
      protected 'observables' => 
        array (size=0)
          empty
      protected 'with' => 
        array (size=0)
          empty
      protected 'morphClass' => null
      public 'exists' => boolean false
      protected 'forceDeleting' => boolean false
1

1 Answers

3
votes

Either use the normal syntax:

User::find(1)->accounts()->get()

Or by Dynamic properties:

User::find(1)->accounts

But don't mix the two up. accounts() is just the hasMany relationship for query builder. The actual objects of Account are retrieved by get(). Dynamic properties shorten the code you need to write for this task

Just out of curiosity, why do you explicitly write out the fully qualified namespace? You can just use extends Eloquent