Php 修改资源时,无法将ServerBag转换为字符串
我真的不明白为什么会发生这种情况,但它告诉我ServerBag无法转换为字符,我真的不明白ServerBag是什么以及为什么会发生这种错误 错误: …\vendor\laravel\framework\src\illumb\Support\Str.php354 相关代码(功能更新): 如果我删除以下几行,代码就可以工作Php 修改资源时,无法将ServerBag转换为字符串,php,laravel,Php,Laravel,我真的不明白为什么会发生这种情况,但它告诉我ServerBag无法转换为字符,我真的不明白ServerBag是什么以及为什么会发生这种错误 错误: …\vendor\laravel\framework\src\illumb\Support\Str.php354 相关代码(功能更新): 如果我删除以下几行,代码就可以工作 $request->validate([ 'username' => 'required|string|max:50', '
$request->validate([
'username' => 'required|string|max:50',
'password' => 'required|string|max:50',
'port' => 'max:5',
'server' => 'string|nullable|max:50',
'hostname' => 'string|nullable|max:100',
'ipvmware' => 'string|nullable|ipv4',
'obs' => 'string|nullable|max:500',
]);
$host = Host::find($id);
$host->username = $request->username;
$host->password = $request->password;
$host->estado = 1;
$host->save();
dd($request->all()):
问题在于您的“如果”条件
解决方案
然后你应该得到输入
现在,关于方法:
执行$request->has(…)
时,您正在检查请求是否包含给定的输入项键。这就是该方法的作用:
# trait InteractsWithInput.php
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
如您所见,它只返回一个布尔值
执行$request->input(…)
时,您试图从请求中获取输入:
if ($request->has('name')) {
//
}
# trait InteractsWithInput.php
/**
* Retrieve an input item from the request.
*
* @param string|null $key
* @param string|array|null $default
* @return string|array|null
*/
public function input($key = null, $default = null)
{
return data_get(
$this->getInputSource()->all() + $this->query->all(), $key, $default
);
}
此方法的功能更强大,因为它可以使用点表示法获取嵌套数据(与$request->get()
one相反)。查看更多详细信息
这将返回一个字符串
,数组
或空
现在,这就是错误所在的位置(您可以在错误跟踪中检查是否存在)。该方法似乎试图将给定对象视为字符串,以便解析if语句(我真的不知道,因为无法访问完整的错误详细信息)
因此,首先,检查请求是否具有特定属性,然后检索输入以执行所需操作。您的
$request
字段之一包含一个标题数组,请参阅:更新我的问题我认为这提供了一种解决问题的方法。因此,在删除ifs语句后,错误消失?是的,但我不明白为什么,是否有其他方法可以查看字段是否为空?也许是通过使用函数$request->input()试试:dd($request->all())
我明白了,你知道每个函数之间的区别吗?
if ($request->has('name')) {
//
}
# trait InteractsWithInput.php
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
# trait InteractsWithInput.php
/**
* Retrieve an input item from the request.
*
* @param string|null $key
* @param string|array|null $default
* @return string|array|null
*/
public function input($key = null, $default = null)
{
return data_get(
$this->getInputSource()->all() + $this->query->all(), $key, $default
);
}