xelyne's avatar

update image, but the image did not moved to the folder

can anyone help me??

im new on laravel 5.4 so, i want to update some profil, when im click update data like name, date birth .. etc got saved to database, but the image did not move to the folder and the name for the image change to some number,

im already trying, the data got saved but name for image still same did not change to number and the image not moved to the folder..... i have searching on go*gle but still not found for the solution. it's making my head becoming stress.

thank you, sorry for my bad english

//ROUTE

//Route::get('/peserta', 'pesertaController@index'); //Route::get('/peserta/daftar', 'pesertaController@daftar'); //Route::get('/peserta/{peserta}', 'pesertaController@detail'); //Route::get('/peserta/{peserta}/edit', 'pesertaController@edit'); //Route::patch('/peserta/{peserta}', 'pesertaController@update'); //Route::get('/peserta/{peserta}/delete', ['as' => 'peserta.delete', 'uses' => 'pesertaController@delete']); //Route::post('/peserta', 'pesertaController@store');

//FORM

//{!! Form::model($peserta, ['method' => 'PATCH', 'action' =>['pesertaController@update', $peserta->id]]) !!}

//UPDATE FUNCTION

//public function update($id, Request $request) {

  $peserta = Peserta::findOrFail($id);
  
//foto = image file
    //upload new image
    if ($request->hasFile('foto')) {
      /* ---------------------------------------------------------------------------------
      | request file
      ---------------------------------------------------------------------------------  */
      $foto = $request->file('foto');
      /* ---------------------------------------------------------------------------------
      | creating file extension for image
      ---------------------------------------------------------------------------------  */
      $ext = $foto->getClientOriginalExtension();
      /* ---------------------------------------------------------------------------------
      | if the image is accepted
      ---------------------------------------------------------------------------------  */
      if ($request->file('foto')->isValid()) {
        /* ---------------------------------------------------------------------------------
        | changing file image name to date for saving to database
    | nama_foto = photo name
        ---------------------------------------------------------------------------------  */
        $nama_foto = date('YmdHis'). ".$ext";
        /* ---------------------------------------------------------------------------------
        | where the folder for the image
        ---------------------------------------------------------------------------------  */
        $upload_path = public_path('upload/');
        /* ---------------------------------------------------------------------------------
        | moving file to folder image
        ---------------------------------------------------------------------------------  */
        $request->file('foto')->move($upload_path, $nama_foto);
        /* ---------------------------------------------------------------------------------
        | saved the file image name to database
        ---------------------------------------------------------------------------------  */
        $peserta->foto = $nama_foto;
      }
    }

  //updating
  $peserta->update($request->all());
  //return
  return redirect('peserta');
}

0 likes
0 replies

Please or to participate in this conversation.