如何解决laravel中保存图像的问题 [英] How to solve the problem of saving image in laravel

查看:81
本文介绍了如何解决laravel中保存图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像保存到数据库中,它正在移动到公用文件夹中,但是没有保存到数据库中.但是,除了图像以外,其他所有东西都工作正常.当调用save()方法时,它给我下面的错误..

I am trying to save my image into database, it was moving into public folder but it doesnot saving into database. But while except image everything is working fine. And when save() method is called it gives me below error. .

这是我在控制器中尝试过的代码

Here what i have tried my code in controller

$student=new Student;
        $student->name = $request->input('name');
        $student->username = $request->input('username');
        $student->email = $request->input('email');
        $student->password = bcrypt($request->input('password'));
        $student->gender = $request->input('gender');
        $student->phone = $request->input('phone');
        if($request->hasFile('image'))
        {
            $file=$request->File('image');
            $ext=$file->getClientOriginalExtension();
            $filename=$student->username . '.' . $ext;
            $file->move('images/',$filename);
            $student->image=$filename;
        }
        $student->save();
        $student->subjects()->attach($request->id);
        return back()->with('msg','Succesfully Added');

推荐答案

使用clientExtension()而不是getClientOriginalExtension()并确保您的路径正确,在这里我定义了public路径.

use clientExtension() instead of getClientOriginalExtension() and make sure your path is correct here I defined the path of public.

if ($request->hasFile('image')) {
    $file = $request->image;
    $destinationPath = public_path().'/images/';
    $filename= $student->username . '.'.$file->clientExtension();
    $file->move($destinationPath, $filename);
    $student->image=$filename;

}

这篇关于如何解决laravel中保存图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆