簡體   English   中英

雄辯的ORM關系

[英]Eloquent ORM relationships

我有兩種模式:帳戶:

<?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實例,但是該實例不包含該用戶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

使用普通語法:

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

或通過動態屬性

User::find(1)->accounts

但是不要將兩者混為一談。 accounts()只是查詢構建器的hasMany關系。 Account的實際對象由get()檢索。 動態屬性縮短了您需要為此任務編寫的代碼

出於好奇,為什么要顯式寫出完全限定的名稱空間? 您可以只使用extends Eloquent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM