laravel 中未定义的数组键 "name_of_firm"?
Undefined array key "name_of_firm" in laravel?
这是我的 RegisterController(Laravel ui 已编辑)。请帮我。
我是否必须为这个问题添加任何其他页面。
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
你应该使用 data_get
或 Arr::get()
助手来解决不存在的元素:
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => data_get($data, 'name_of_firm'),
'number' => data_get($data, 'number'),
'address' => data_get($data, 'address')],
]);
}
如果 name_of_firm
可以为空或必需
则有 2 种情况
案例:1
如果需要 name_of_firm
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'name_of_firm' => ['required']
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
案例:2
如果 name_of_firm
可以为空
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'] ?? null, // make sure database is accept null value
'number' => $data['number'],
'address' => $data['address'],
]);
}
这是我的 RegisterController(Laravel ui 已编辑)。请帮我。 我是否必须为这个问题添加任何其他页面。
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
你应该使用 data_get
或 Arr::get()
助手来解决不存在的元素:
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => data_get($data, 'name_of_firm'),
'number' => data_get($data, 'number'),
'address' => data_get($data, 'address')],
]);
}
如果 name_of_firm
可以为空或必需
案例:1
如果需要 name_of_firm
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'name_of_firm' => ['required']
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
案例:2
如果 name_of_firm
可以为空
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'] ?? null, // make sure database is accept null value
'number' => $data['number'],
'address' => $data['address'],
]);
}