简体   繁体   English

无法在laravel 5.1中更新映像?

[英]Can not update image in laravel 5.1?

Any one can help me with this? 有人可以帮助我吗? I try to update new image for my project but, it get errors.If I don't choose new image just update content only it's ok, but with new image I cannot update. 我尝试为我的项目更新新图像,但是会出错。如果我不选择新图像,只更新内容就可以了,但是使用新图像我无法更新。 This is my code 这是我的代码

public function updateStudent(UpdateStudentRequest $request){//(Request $request)
    $id = $request->input('id');
    $student = Students::find($id);

    if( $request->hasFile('pic') ){
    $path = 'images/';
    $file = Image::make(input::file('pic'));
    $fileName = Input::file('pic')->getClientOriginalName();
    $file->save($path."original/".$fileName);
    $students->original = $path."original/".$fileName;
    //get the desire image size
    $file->fit(120,90);
    $file->save($path."fit/".$fileName);
    $students->image = $path."fit/".$fileName; 
    }

    $student->first_name = $request->input('first_name');
    $student->last_name = $request->input('last_name');
    $student->email = $request->input('email');
    $student->password = $request->input('password');
    $student->address = $request->input('address');

    $student->save();
    }

This is the error: 这是错误:

ErrorException in Crud.php line 73:
Creating default object from empty value

It seems that your $student variable is empty. 您的$ student变量似乎为空。 You get this error when you try to assign a value to an attribute of an object, but the object reference is null. 当您尝试将值分配给对象的属性,但是对象引用为null时,会出现此错误。

In the future use Student::findOrFail($id) instead of Student::find($id) , this way you'll make sure that if object could not be found you'll get an exception. 将来使用Student :: findOrFail($ id)代替Student :: find($ id) ,这样可以确保如果找不到对象,您将获得异常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM