我正在尝试在我的应用程序中使用Form Request
注册用户,但是我收到此错误:
Argument 1 passed to Illuminate\Database\Eloquent\Model::create()
must be of the type array, object given,
called in /var/www/app/app/Http/Controllers/RegisterNewUsersPgController.php
on line 28 and defined
我的路线:
Route::post('registerRealUserAccount' ,'RegisterNewUsersPgController@registerRealUserAccount');
控制器:
class RegisterNewUsersPgController extends Controller
{
/*
* RegisterRealUserAccount
*
* @param StoreRegisterRealUserAccount $request
*/
public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
{
AccountRealUsers::create($request);
}
}
StoreRegisterRealUserAccount
上课:
class StoreRegisterRealUserAccount extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
];
}
}
AccountRealUsers
型号:
class AccountRealUsers extends Model
{
protected $table = 'account_reaL_users';
protected $fillable = [
'acceptor_farsi_name',
'acceptor_english_name',
'display_name',
'username',
'password',
'service_type',
];
protected $guarded = ['users'];
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
}
答案 0 :(得分:1)
create()
函数需要一个数组作为参数,而不是一个对象($request
)。 $request->all()
方法应该返回所有请求参数的数组。
class RegisterNewUsersPgController extends Controller
{
/*
* RegisterRealUserAccount
*
* @param StoreRegisterRealUserAccount $request
*/
public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
{
AccountRealUsers::create($request->all());
}
}
您可能需要考虑指定要存储的实际参数,因为额外的GET / POST参数可能会破坏您的代码。
如果您想指定参数,请尝试以下操作......
class RegisterNewUsersPgController extends Controller
{
/*
* RegisterRealUserAccount
*
* @param StoreRegisterRealUserAccount $request
*/
public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
{
AccountRealUsers::create($request->only([
'acceptor_farsi_name',
'acceptor_english_name',
'display_name',
'username',
'password',
'service_type',
]));
}
}
上面只会从$request
对象获取指定的请求参数,并将它们作为数组返回。