Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法更新Laravel中$request数组的元素_Php_Laravel - Fatal编程技术网

Php 无法更新Laravel中$request数组的元素

Php 无法更新Laravel中$request数组的元素,php,laravel,Php,Laravel,好的,这是我的控制器中的存储方法: public function store(EmployeeRequest $request) { if($request->hasFile('photo')){ $path = $request->photo->store('employeeimages'); // This should have changed the element in the $request['photo'] index:

好的,这是我的控制器中的存储方法:

public function store(EmployeeRequest $request)
{
    if($request->hasFile('photo')){
        $path = $request->photo->store('employeeimages');
// This should have changed the element in the $request['photo'] index:
        $request->photo = $path; 
    dd($request->all());
    }
    Employee::create($request->all());
    return redirect('dashboard');
}

我以为
$request->photo
可以让我们访问数组的
$request['photo']
元素,所以我尝试通过
$request->photo=$path更新它
但是当我死后转储
$request->all()
$request['photo']
不会更新,并且仍然保留对先前值的引用,该值是
UploadedFile
类的一个实例。如何更改$request数组的元素?

将输入存储到新变量,然后对其进行修改可以解决问题。这就是我所做的:

public function store(EmployeeRequest $request)
{
    if($request->hasFile('photo')){
        $input = $request->all();
        $path = $request->photo->store('employeeimages');
        $input['photo'] = $path;
    }
    Employee::create($input);
    return redirect('dashboard');
}

您还可以使用
$request->merge($array)