Ishatanjeeb's avatar

Image upload file does not working

My from is like below:

<form method="post" enctype="multipart/form-data" action="/admin/projects" class="col-lg-8">
    {{csrf_field()}}
    <div class="form-group">
        <label for="title">Proejct Name:</label>
        <input type="text" name="title" id="title" class="form-control" value="{{old('title')}}">
    </div>
    <div class="form-group">
        <label for="sub_title">Project Sub_title:</label>
        <input type="text" name="sub_title" id="sub_title" class="form-control" value="{{old('sub_tile')}}">
    </div>
    <div class="form-group">
        <label for="photo">Photo:</label>
        <input type="file" name="file" id="file" class="form-control" value="{{old('file')}}">
    </div>
    <button type="submit">Submit</button>
    @if(count($errors)>0)
        <div class="alert alert-danger">
            <ul>
                @foreach($errors->all()as $error)
                    <li>{{$error}}</li>
                @endforeach
            </ul>
        </div>
    @endif
</form>

and my controller:

public function store(ProjectRequest $request)
    {
        $file=$request->file('file');
        $name=time().$file->getClientOriginalName();
        $file->move('images/client',$name);
        Project::create(['photo'=>"images/client/{$name}"]);
        $request->all();
        Project::create($request->all());
        return 'Done';
    }

image path does not create on database.Please help me

0 likes
11 replies
Ishatanjeeb's avatar

I think it's working but have but save on database like this "resource://jid0-gxjllfbcoax0lcltedfrekqdqpi-at-jetpack/as-ff/data/edit.html"

RachidLaasri's avatar
public function store(ProjectRequest $request)
{
    $project = Project::fill($request->all());

    $file = $request->file('file');
    $name = time().$file->getClientOriginalName();
    $file->move('images/client', $name);

    $project->photo = "images/client/{$name}";

    $project->save();

    return 'Done';
}
Ishatanjeeb's avatar

Showing error

Non-static method Illuminate\Database\Eloquent\Model::fill() should not be called statically, assuming $this from incompatible context
RachidLaasri's avatar
Level 41

This should work:

public function store(ProjectRequest $request)
{
    $file = $request->file('file');
    $name = time().$file->getClientOriginalName();
    $file->move('images/client', $name);

    $data = array_merge(['photo' => "images/client/{$name}"], $request->all());

    Project::create($data);

    return 'Done';
}
2 likes
Folarin's avatar

RachidLaasri with your method I get the file uploaded into the right folder in my public directory but the correct file path is not persisted in the db instead '/private/var/tmp/randomfilename' is what goes in the db. Any idea why this is happening?

michaeldunga's avatar

This is for others who need help with this, just go to php.ini in xampp and change the following to this:

upload file size max:

  • upload_max_filesize = 50M
  • post_max_size = 50M
  • max_input_time = 300
  • max_execution_time = 300

Please or to participate in this conversation.