laravel中如何解决文件问题

时间:2019-11-01 16:28:13

标签: php laravel

我正在使用观察器类来存储图像,一切正常,例如图像存储在公用文件夹中,保存在数据库中,但保存后会出现如下错误enter image description here

我在学生观察员课堂上尝试过

 protected $request;

    public function __construct(Register $request)
    {
        $this->request = $request;
    }
    public function created(Student $student)
    {
        //
    }
    public function creating(Student $student)
    {
        //dd($request->image);
        if ($this->request->hasFile('image')) {
            $file = $this->request->image;
            $destinationPath = public_path().'/images/';
            $filename= $student->username . '.'.$file->clientExtension();
            $file->move($destinationPath, $filename);
            $student->image=$filename;

        }
    }

在我的控制器中

public function create(Register $request)
    {
        $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');
        $student->save();
        $student->subjects()->attach($request->id);
        return home('home');
    }

0 个答案:

没有答案