can't update photo in Laravel because validation even can upload photo when create a profile person

I can't upload a photo when update a profile even i can upload it when i create profile in first time. In Update section, i put validation in form request for photo update and create. It's

'foto'          => 'sometimes|image|max:500|mimes:jpeg,jpg,bmp,png',

Eveything good when upload in create. But, when upload for updating, warning appears because validation. Such 'The PHOTO must be an image.' or 'The PHOTO must be a file of type: jpeg,jpg,bmp,png.'

This my code :

On Controller for update :

public function update(Siswa $siswa, SiswaRequest $request){
        $input = $request->all();

        if($request->hasFile('foto')) {

          $exist = Storage::disk('foto')->exists($siswa->foto);
          if(isset($siswa->foto) && $exist) {
            $delete = Storage::disk('foto')->delete($siswa->foto);
          }

          $foto = $request->file('foto');
          $ext  = $foto->getClientOriginalExtension();

          if ($request->file('foto')->isValid()) {
            $foto_name = date('YmdHis').".$ext";
            $upload_path = 'fotoupload';
            $request->file('foto')->move($upload_path, $foto_name);
            $input['foto'] = $foto_name;
          }
        }

        $siswa->update($input);

        $telepon = $siswa->telepon ?? new Telepon();
        $telepon->nomor_telepon = $request->input('nomor_telepon');
        $siswa->telepon()->save($telepon);

        $siswa->hobi()->sync($request->get('hobi_siswa', []));

        return redirect('siswa');
    }

This my code for validation in request

public function rules()
    {
      if($this->method() == 'PATCH') {
        $id_rules      = 'required|numeric|digits:8|unique:siswa,id,' . $this->get('id');
        $telepon_rules = 'sometimes|numeric|digits_between:10,15|unique:telepon,nomor_telepon,' . $this->get('id') . ',id_siswa';
      }
      else {
        $id_rules      = 'required|numeric|digits:8|unique:siswa,id';
        $telepon_rules = 'sometimes|numeric|digits_between:10,15|unique:telepon,nomor_telepon';
      }
        return [
              'id'            => $id_rules,
              'nama_siswa'    => 'required|regex:/^[\pL\s]+$/u|max:30',
              'tanggal_lahir' => 'required|date',
              'jenis_kelamin' => 'required|in:L,P',
              'nomor_telepon' => $telepon_rules,
              'id_kelas'      => 'required',
              'foto'          => 'sometimes|image|max:500|mimes:jpeg,jpg,bmp,png',
        ];
    }

So, i can't update my photo at all. How is the way to fix it?

1 answer

  • answered 2018-07-11 02:34 hinteractive02

    Add this to your form. Maybe this will fix it.

    enctype="multipart/form-data" :

    <form action="..." class="...." method="post" enctype="multipart/form-data">