Php 只有用户模型使用空值保存到数据库
在Laravel5.4中,当我尝试将用户模型保存到数据库时,值不会被保存。我还设置了fillable属性 它在拉威尔5.3中工作。此问题是在将应用程序升级到Laravel 5.4之后出现的 下面是一个用户模型Php 只有用户模型使用空值保存到数据库,php,laravel,eloquent,laravel-5.4,Php,Laravel,Eloquent,Laravel 5.4,在Laravel5.4中,当我尝试将用户模型保存到数据库时,值不会被保存。我还设置了fillable属性 它在拉威尔5.3中工作。此问题是在将应用程序升级到Laravel 5.4之后出现的 下面是一个用户模型 class User extends BaseModel implements AuthenticatableContract, CanResetPasswordContract, JWTSubject { use SoftDeletes, UserAccess,
class User extends BaseModel implements AuthenticatableContract, CanResetPasswordContract, JWTSubject
{
use SoftDeletes,
UserAccess,
UserAttribute,
UserRelationship,
Authenticatable,
CanResetPassword,
Notifiable;
/**
* Database Table
*
* @var string
*/
protected $table = "users";
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
/**
* Fillable Form Fields
*
* @var array
*/
protected $fillable = [
'name',
'first_name',
'last_name',
'email',
'password',
'status',
'confirmed',
'api_user',
'confirmation_code',
'account_id',
'role_id',
'cw_contact_id',
'all',
'all_locations',
'username',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Select HTML Preference
*
* @var string
*/
protected static $selectHTMLFormat = "[email]";
/**
* @var array
*/
protected $dates = ['deleted_at', 'last_login'];
}
请注意,问题仅限于用户型号
我正在保存用户,如下所示
// Create User
$user = $this->model->create([
'first_name' => $input['first_name'],
'last_name' => $input['last_name'],
'username' => $input['username'],
'email' => $input['email'],
'password' => bcrypt($input['password']),
'confirmation_code' => md5(uniqid(mt_rand(), true)),
'confirmed' => 1,
'api_user' => (isset($input['api_user']) ? $input['api_user'] : 0),
'account_id' => $input['account_id'],
'role_id' => (isset($input['role_id']) ? $input['role_id'] : 0),
'all' => (!isset($input['associated-permissions']) || $input['associated-permissions'] == 'all') ? 1 : 0,
'status' => (!isset($input['status']) || $input['status'] ? 1 : 0),
'all_locations' => $input['all_locations']
]);
然后将调用BaseModel的create方法,下面是它的代码
public static function create(array $attributes = Array())
{
$user = access()->user();
if($user)
{
$attributes['account_id'] = (!isset($attributes['account_id']) ? $user->account->id : $attributes['account_id'] );
}
$childClass = get_called_class();
$model = new $childClass;
$model->runActionLogger(false, 'create');
return parent::query()->create($attributes);
}
从5.4开始,create()
作为dinamic方法调用处理,即通过调用其中一个函数(取决于是否静态调用):
在<代码>照明\数据库\雄辩\模型
类中
现在我没有您的所有代码,但是,我将尝试在您的BaseModel类中更改这一行:
return parent::query()->create($attributes);
为此:
return $model->create($attributes);
或者,对我来说更好的是:
return (new static)->newQuery()->create($attributes);
在
它说:
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);
所以
其中,users()
是您的公共功能,但我不知道在您的情况下,$this
是什么,但应该是文档示例中的模型
为什么不使用资源控制器?或者,如果需要填充数据库,请使用播种器
我认为它将更易于管理。原因很可能是Laravel 5.4中的新中间件,称为“请求清理中间件”,如中所述 在app/Http/kernel.php中禁用
\illumb\Foundation\Http\Middleware\convertEmptyStringsToull::class,
,然后查看您得到了什么
您还可以签入/config/database.php和您的mysql连接设置:'strict'=>true
,如果是,请设置为false
一个好的实践是使用模型进行用户输入。在这种情况下,您可以使用以下内容填充模型,而不是$user=$this->model->create(…)
$user=new\App\user($input)
并从那里更新您的值,f.ex。
$user->confirmation_code=md5(uniqid(mt_rand(),true))代码>和
$user->password=bcrypt($user->password)代码>
如果字段可为空,请在迁移文件f.ex中指明<代码>$table->string('all')->nullable()代码>
如果完成,只需运行$user->save()代码>所以我可以考虑两件事
第一,没有必要使用
受保护的$guarded=[]
及
受保护的$fillable=[]
Guarded将假设所有内容都是可填充的,如果不在这里,fillable将假设所有内容都是受保护的,除非在这里
引用文件
虽然$fillable用作应可批量分配的属性的“白名单”,但您也可以选择使用$GARDED。$guarded属性应该包含一个属性数组,您不希望这些属性是可批量分配的。不在数组中的所有其他属性都是可分配质量的。因此,它的功能就像一个“黑名单”。当然,您应该使用$fillable或$guarded,而不是两者都使用
第二,要排除任何$this->model元素,请尝试先实例化类并保存它们
use App\Path\To\Model as user;
$user = new user();
$user->first_name = $input['first_name'];
$user->last_name = $input['last_name'];
$user->username = $input['username'];
$user->email = $input['email'];
$user->password = bcrypt($input['password']);
$user->confirmation_code = md5(uniqid(mt_rand(); true));
$user->confirmed = 1;
$user->api_user = (isset($input['api_user']) ? $input['api_user'] : 0);
$user->account_id = $input['account_id'];
$user->role_id = (isset($input['role_id']) ? $input['role_id'] : 0);
$user->all = (!isset($input['associated-permissions']) || $input['associated-permissions'] == 'all') ? 1 : 0;
$user->status = (!isset($input['status']) || $input['status'] ? 1 : 0);
$user->all_locations = $input['all_locations'];
$user->save();
伙计们,我可以用Fill()方法解决这个问题
我还错误地在CanResetPassword特性上添加了构造,这也导致了问题。所以,如果我删除这个选项,一切都会像以前一样工作。是的,你不能在Traits中使用构造方法
关于trait的更多细节,请参考PHP.net,他们说“在构造方法(可能还有其他神奇的方法)上使用AS是非常非常糟糕的。”
你可以用下面的方法使用trait
class User extends BaseModel
{
use userRelation
}
Trait userRelation
{
public function getUserName()
{
return "Jhon Doe";
}
}
我已经创建了“userRelation”特性,它包含很少有用的代码可重用
详情请参阅以下连结-
请试试看,如果不行请告诉我。
谢谢问题是什么?向我们展示您如何在用户模型中保存一切看起来都正常(没有特征)的代码。您可以使用try-catch块捕获堆栈跟踪错误,并请与我们共享结果。@SteD在我的问题中提到过。如何保存用户。当调用$user=$this->model->create
时,$this
是什么?当我使用$model->create()时,它将只调用model baseModel函数,而不调用Laravel函数。另一种方法不适用于用户模型。请尝试最后一种方法,但不会成功。我想这是一个密码合同的例子。如果我没有实现它,上面的代码将起作用。事实并非如此。中间件并非如此。上述代码适用于所有其他型号。只是用户模型有问题。我不想这样做。因为我正在记录每个活动,所以我只需要绕过它baseModel。它可以与其他模型一起工作,但问题仅限于用户模型。@ViralSolani您可以发布更多代码,只是为了理解什么是$this
?我不想这样做。因为我正在记录每个活动,所以我只需要绕过它baseModel。它正在与其他模型一起工作,但问题仅限于用户模型。
use App\Path\To\Model as user;
$user = new user();
$user->first_name = $input['first_name'];
$user->last_name = $input['last_name'];
$user->username = $input['username'];
$user->email = $input['email'];
$user->password = bcrypt($input['password']);
$user->confirmation_code = md5(uniqid(mt_rand(); true));
$user->confirmed = 1;
$user->api_user = (isset($input['api_user']) ? $input['api_user'] : 0);
$user->account_id = $input['account_id'];
$user->role_id = (isset($input['role_id']) ? $input['role_id'] : 0);
$user->all = (!isset($input['associated-permissions']) || $input['associated-permissions'] == 'all') ? 1 : 0;
$user->status = (!isset($input['status']) || $input['status'] ? 1 : 0);
$user->all_locations = $input['all_locations'];
$user->save();
public static function create(array $attributes = Array())
{
$user = access()->user();
if($user)
{
$attributes['account_id'] = (!isset($attributes['account_id']) ? $user->account->id : $attributes['account_id'] );
}
$childClass = get_called_class();
$model = new $childClass;
$model->fill($attributes);
$model->save();
$model->runActionLogger($model, 'create');
return $model;
}
class User extends BaseModel
{
use userRelation
}
Trait userRelation
{
public function getUserName()
{
return "Jhon Doe";
}
}