lily
lily

Reputation: 199

How solve the problem with saving file in Laravel?

I have problem with saving file in my database I have done the coding but somehow the coding did not read my first line code of File function because it keeps reading the else line which will save noimage.jpgif ,no file was detected during the submit. I think the problem is in my blade. But I can't figured it out.

enter image description here

The name of the file also did not appear after I done choosing the file.

The data also saved into my database but with noimage.jpg name even I have try to submit the image.

 public function store(Request $request)
    {
      
       $this->validate($request,[
            'location' =>'required',
            'event_image'=>'image|nullable|max:1999',
            'availability'=>'required',
        ]);

        //handle the file upload
        if($request ->hasFile('event_image')){   // to check if user has opted to upload the file
        
            // get filename with extention
            $filenameWithExt = $request->file('event_image')->getClientOriginalName();

            //get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            //get ext (extention)
            $extension =$request->file('event_image')->getClientOriginalExtension();
            //filename to store
            $fileNameToStore = $filename .'_'.time().'.'.$extension;
            //upload image
            $path = $request->file('event_image')->storeAs('public/event_images',$fileNameToStore);
       }else{
           $fileNameToStore ='noimage.jpg'; // if user dont have any file this name will be put on the database
       }

       $event = new Event;
       $event->location = $request->input('event_location');
       $event->event_image =$fileNameToStore;
       $event->availability =$request->input('event_availability');
       $event->admin_id = auth()->user()->id;

       $event->save();
}

<form class="form-horizontal" action="{{route('event.store')}}" method="POST" enctype="multipart/form-data">
              @csrf
 <div class="form-group row">
                    <label for="event_image" class="col-sm-2 col-form-label">Event Picture</label>
                    <div class="col-sm-10">
                      <div class="custom-file">
                        <input type="file" class="custom-file-input" id="customFile">
                        <label class="custom-file-label" for="customFile" name="event_image" id="event_image"></label>
                      </div>
                    </div>
                  </div>
</div>
</form>


Upvotes: 0

Views: 526

Answers (2)

A.A Noman
A.A Noman

Reputation: 5270

You have to use name for getting form data

It should be

<input type="file" class="custom-file-input" id="customFile" name="event_image ">

Upvotes: 0

kewlashu
kewlashu

Reputation: 1109

I think the issue is in html input type file element, it has name attribute missing:

This:

<input type="file" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile" name="event_image" id="event_image"></label>

Should be like this:

<input type="file" class="custom-file-input" name="event_image" id="customFile">
                        <label class="custom-file-label" for="customFile"  id="event_image"></label>

Upvotes: 1

Related Questions