1

what happen to my update file?

I can't update file in my program, what's wrong with my controller?

edit.blade.php

<div class="row">
    <div class="col-12">
        {{ Form::model($activityreports,['route'=>['activityreports.update',$activityreports['id']], 'files'=>true, 'method'=>'GET', 'enctype'=>'multipart/form-data']) }}
        <div class="card">
            <div class="card-body">
                @if(!empty($errors->all()))
                <div class="alert alert-danger">
                    {{ Html::ul($errors->all())}}
                </div>
                @endif
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            {{ Form::label('file', 'Laporan Kegiatan') }}
                            {{ Form::file('file', ['class'=>'form-control']) }}        
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- </form> -->
        {{ Form::close() }}
    </div>
</div>

controller

public function update(Request $request, $id)
    {
        //
        $rules=[
             
            'title'=>'required',
            'date'=>'required',
            'type'=>'required',
            'place'=>'required'
        ];
 
        $pesan=[
            'title.required'=>'Judul Kegiatan Tidak Boleh Kosong',
            'date.required'=>'Tanggal Kegiatan Tidak Boleh Kosong',
            'type.required'=>'Jenis Kegiatan Tidak Boleh Kosong',
            'place.required'=>'Lokasi Kegiatan Tidak Boleh Kosong',
        ];
 
 
        $validator=Validator::make(Input::all(),$rules,$pesan);
 
        if ($validator->fails()) {
            return Redirect::to('admin/activityreports/'.$id.'/edit')
            ->withErrors($validator);
 
        }else{
            $fileName="";
 
            $activityreports=Activityreports::find($id);

            if($request->hasFile('file')){
                Storage::delete($activityreports->file);

                $file=$request->file('file');
                $fileName=$file->getClientOriginalName();
                $file->move('storage/file/activityreportsFile/', $fileName);
                $activityreports->file=$fileName;  
            }
                
            $activityreports->title=Input::get('title');
            $activityreports->date=Input::get('date');
            $activityreports->type=Input::get('type');
            $activityreports->place=Input::get('place');
            $activityreports->save();
 
            Session::flash('message','Data Berhasil Diubah');
             
            return Redirect::to('admin/activityreports/index');
        }

I tried another code and there is error 'Call to member function getClientOriginalName on null. And when I make file required then input file, there is always warning to input .pdf file.

1 Answer 1

0

First, wehre is the problem which causes the error:

This line $file=$request->file('file'); sets $fileto null, therefore you cannot call the function in the next line. This means that no file is present in your request. You can check your request if you call dd($request->all()) in your update method.

Second, why does this happen?

I think your problem is that you create a GET form in your view and therefore a GET request will be sent. You will probably have to make a POST request (you could also make a PATCH request since you call the update method in your controller).

Please change the form to POST and your route to POST and try it again.

Not the answer you're looking for? Browse other questions tagged or ask your own question.