代码之家  ›  专栏  ›  技术社区  ›  Lucas Green

为什么Laravel 5.6/Eloquent不同意它自己的数据库连接?

  •  1
  • Lucas Green  · 技术社区  · 7 年前

    我的应用程序在生产环境中表现出这种奇怪的行为,而在其他任何地方(在实际界面和修补程序中都不会发生):

    >>> $db = DB::connection();
    => Illuminate\Database\MySqlConnection {#832}
    >>> \App\User::resolveConnection()->select('select * from users');
    => [
         {#838
           +"id": 2,
           +"deleted_at": "2018-04-10 20:47:07",
           ...perfectly normal data
         },
         {#848
           +"id": 3,
           +"deleted_at": "2018-04-10 20:47:07",
           ...perfectly normal data
         },
       ]
    >>> \App\User::resolveConnection()->select('select * from users');
    => [
         {#846
           +"id": 2,
           ...perfectly normal data
         },
         {#839
           +"id": 3,
           ...perfectly normal data
         },
       ]
    >>> \App\User::all();
    => Illuminate\Database\Eloquent\Collection {#861
         all: [],
       }
    

    用户类非常简单:

    namespace App;
    
    use Laravel\Passport\HasApiTokens;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Spatie\Permission\Traits\HasRoles;
    use Laravel\Cashier\Billable;
    
    class User extends Authenticatable
    {
        use SoftDeletes;
        use Billable;
        use HasApiTokens, Notifiable, HasRoles;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password', 'status'
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    
        public function organization()
        {
            return $this->hasOne(Organization::class);
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   fubar    7 年前

    我的第一个想法是,您在 User 模型因此,您的所有用户都被软删除了吗?

    Eloquent将自动应用相关的软删除查询范围来过滤已删除的模型,而标准DB查询则不会。

    等效的DB查询为:

    SELECT * FROM users WHERE deleted_at IS NULL