简体   繁体   English

CRUD(更新)上载文件出现问题“对成员函数getClientOriginalName()的调用为null”

[英]Problem with CRUD(update) Upload File “call to a member function getClientOriginalName() on null”

I want to Update an image using Laravel storage file system in my admin data. 我想在管理数据中使用Laravel存储文件系统更新映像。 However, there's an error when I attempt to upload an image 但是,当我尝试上传图像时出现错误

Iam using Laravel 5.7 Iam使用Laravel 5.7

Here is my create, the create is success 这是我的创造,创造就是成功

public function store(Request $request)
    {

        //
        $product = new \App\Product;
        $product->product_name = $request->get('product_name');
        $product->desc = $request->get('desc');
        $product->stock = $request->get('stock');
        $product->price = $request->get('price');
        $product->category = $request->get('category');



        $img = $request->file('img');

        $new_name = rand() . '.' . $img->getClientOriginalExtension();
        $img->move(public_path('img'), $new_name);
        $product->img = $new_name;
        $product->save();

        return redirect('admin')->with('success', 'Data Produk telah ditambahkan'); 
    }

Here is my update 这是我的更新

public function update(Request $request, $id)
    {
        //
        $product = $request->all();
        $product= \App\Product::find($id);
        $new_name = $request->file('img')->getClientOriginalName();
        $destinationPath = 'img/';
        $proses = $request->file('img')->move($destinationPath, $new_name);

        if($request->hasFile('img'))
        {
            $product = array(
                    'product_name' => $product['product_name'],
                    'desc'=> $product['desc'],
                    'stock'=> $product['stock'],
                    'price'=> $product['price'],
                    'category'=> $product['category'],
                    'img' => $new_name,
                );

            $product->save() ;
            return redirect('admin')->with('success', 'Data Produk telah ditambahkan'); 
        }


    }

Call to a member function getClientOriginalName() on null 在null上调用成员函数getClientOriginalName()

I think there is no image file attach while updating. 我认为更新时没有附加图像文件。 You can use my code as reference. 您可以将我的代码用作参考。 Don't forget to check for the input field in your update field. 不要忘记在更新字段中检查输入字段。 First check if there is image file or not. 首先检查是否有图像文件。 Then, go for getting name, extension and other staff. 然后,去获取姓名,分机号和其他人员。

public function update($id, Request $request)
{
    $input = $request->all();
    $product= \App\Product::find($id);

    if (empty($product)) {
        Flash::error('product not found');

        return redirect(route('products.index'));
    }
    if ($request->hasFile('product_img')) {
        $fileNameWithExt = $request->file('product_img')->getClientOriginalName();
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        $extension = $request->file('product_img')->getClientOriginalExtension();
        $new_product_img = $filename . '_' . time() . '.' . $extension;
        $path = $request->file('product_img')->move('images/products', $new_product_img);
        Storage::delete('products/'.$product->product_img);
        $input['product_img']= $new_product_img;
    }
    $product= $this->productRepository->update($input, $id);

    Flash::success('product updated successfully.');

    return redirect(route('products.index'));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel 5.2 | 上传文件-在null上调用成员函数getClientOriginalName() - laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null Symfony文件上载“错误:在null上调用成员函数getClientOriginalName()” - Symfony file upload “Error: Call to a member function getClientOriginalName() on null” 在null上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on null 在 null laravel 上调用成员函数 getClientOriginalName() - Call to a member function getClientOriginalName() on null laravel 在 null 上调用成员 function getClientOriginalName() 时出错 - Error Call to a member function getClientOriginalName() on null Laravel“文件上传错误调用非对象上的成员函数getClientOriginalName()” - Laravel “File Upload Error Call to a member function getClientOriginalName() on a non-object” php-在null上调用成员函数getClientOriginalName()并删除警报laravel - php - Call to a member function getClientOriginalName() on null and remove alert laravel 在数组laravel上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on array laravel 在数组Laravel 5.2上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on array Laravel 5.2 调用非对象上的成员函数 getClientOriginalName() - Call to a member function getClientOriginalName() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM