Search code examples
phpdatabaselaravel-5.1image-upload

Laravel 5 can't store image name in database


I am using Laravel 5.1

I can move image to my destination folder with correct name but in database it's saved with a temp name, like D:\xampp\tmp\phpC7EA.tmp

form.blade.php

<div class="form-group">
    {!! Form::label('image', 'Upload Image', ['class'=>'control-label col-sm-4']) !!}
    <div class="col-sm-8">
        {!! Form::file('image', null) !!}
    </div>
</div>

TeacherController.php

public function store(TeacherRequest $request)
{
    Teacher::create($request->all());
    $image_name = $request->file('image')->getClientOriginalName();
    $request->file('image')->move(base_path().'/public/images', $image_name);
    Session::flash('success_message', 'Teacher has been added successfully!');
    return redirect('teacher');
}

Solution

  • I am having the same problem when updating the record with new image,image name save in correct name but tmp name in database like 'C:\wamp\tmp\phpD10D.tmp' what i did before saving is

    $request['img'] = $imageName; //with new name
    $images->update($request->all());
    

    I know it should be like $_FILE['img']['name'] = $imageName

    but how to manage it in Laravel 5.1