首页 文章

雄辩的ORM关系

提问于
浏览
0

我有两个模特:账户:

<?php

namespace App\Models;

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

..和用户:

<?php

namespace App\Models;

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

现在,当我执行以下操作时 - var_dump(\App\Models\User::find(1)->accounts()); - 我返回了Illuminate \ Database \ Eloquent \ Relations \ HasMany实例,但它没有't contain my accounts for that user'的ID . 我可以确认我的帐户表确实有几行,其中user_id设置为1.有什么我遵循文档,我看不到任何不同 .

如果有用的话,下面是 var_dump(\App\Models\User::find(1)->accounts()); 的输出:

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 'martyn@yahoo.co.uk' (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 'martyn@yahoo.co.uk' (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 回答

  • 3

    使用正常的语法:

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

    或者Dynamic properties

    User::find(1)->accounts
    

    但不要混淆两者 . accounts() 只是查询构建器的hasMany关系 . 帐户的实际对象由 get() 检索 . 动态属性缩短了为此任务编写所需的代码

    出于好奇,为什么要明确写出完全限定的命名空间?你可以使用 extends Eloquent

相关问题