用户模型始终返回null Laravel 5.1.24

时间:2016-01-06 14:20:27

标签: php laravel-5.1

Here is the output when i used tinker in artisan to query User Model and Team Model.

如上图所示,用户模型总是返回null,而实际上在数据库中有用户的记录。以下是我的用户类

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
// use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Team;
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract
{
  use Authenticatable, Authorizable, SoftDeletes;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';

  protected $primaryKey = 'user_id';

  /**
   * The attributes that are not mass assignable.
   *
   * @var array
   */
  protected $guarded = ['created_at','deleted_at','updated_at'];

  /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_admin' => 'boolean',
        'is_owner' => 'boolean',
    ];

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  //protected $hidden = ['user_pass'];

  public function getRememberToken()
  {
    return null; // not supported
  }

  public function setRememberToken($value){  }

  public function getRememberTokenName()
  {
    return null; // not supported
  }

  /**
   * Overrides the method to ignore the remember token.
   */
  public function setAttribute($key, $value)
  {
    $isRememberTokenAttribute = $key == $this->getRememberTokenName();
    if (!$isRememberTokenAttribute)
    {
      parent::setAttribute($key, $value);
    }
  }
  public function isOwner()
  {
    return ($this->is_owner); 
  }
  public function isAdminOrOwner()
  {
    return ($this->is_admin || $this->is_owner);
  }
}

here is the result of vardump(App\User::all()) and dd(App\User::all())

检查我的数据库后,我发现用户表列deleted_at的默认值为0000-00-00 00:00:00,这使得Laravel的SoftDeletes将每个条目视为已删除,因此返回NULL /空

修正

制作可空deleted_at列。 将所有deleted_at个时间戳设置为NULL。

0 个答案:

没有答案