Skip to content
Advertisement

How to delete file in my database Laravel

How do I delete files in the database, because when I click delete the data in the folder is deleted, but the data in the file_rekap tables don’t want to be deleted, but the ones in the rekap table are deleted

RekapController:

public function delete_rekap($id){
    $data = rekap::findOrfail($id);

    $files = file_rekap::where("rekap_id", $data->id)->get();
    foreach($files as $file){
    if (File::exists("rekap_file/".$file->file)) {
        File::delete("rekap_file/".$file->file);
        }
    }
    $data->delete();
    return redirect()->route('rekap')->with('success','Data berhasil dihapus');
}

public function files($id){
    $data = rekap::find($id);
    if(!$data) abort(404);
    $files = $data->files;
    return view ('file_rekap',compact('data','files'));
}

Model rekap and file_rekap

class rekap extends Model{
    use HasFactory;
    
    protected $fillable = [
        'customer',
        'vessel',
        'scopejob',
        'pergantian_sparepart',
        'teknisi',
        'tahun',
        'keterangan',
    ];

    public function files(){
    return $this->hasMany(file_rekap::class);
        
    }
}
/////////////////////////////////
class file_rekap extends Model
{
    use HasFactory;
    protected $fillable = [
        'file',
        'rekap_id',
    ];

    public function rekaps(){
        return $this->belongsTo(rekap::class);
    }
}

and script in my rekap_blade

                @csrf
                  <div class="modal-body">
                    <div class="form-group">
                      <input type="file" name="import_excel" required>
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save changes</button>
                  </div>
                </form>
            </div>
          </div>
        </div>
          <div class="row g-2 align-items-center m-2">
            <form action="/rekap" method="GET">
            <div class="input-group">
              <input type="text" class="form-control " placeholder="Cari..." name="search">
              <button class="btn btn-info" type="submit">Search</button>
            </div>
          </div>
            </form>
              <table class="table">
                  <thead class="thead ">
                    <tr>
                      <th scope="col">Customer</th>
                      <th scope="col">Vessel/Unit</th>
                      <th scope="col">Scope Job</th>
                      <th scope="col">Pergantian Spare Part</th>
                      <th scope="col">Teknisi</th>
                      <th scope="col">Bulan Tahun</th>
                      <th scope="col">Catatan</th>
                      <th scope="col">Total File</th>
                      <th scope="col">Aksi</th>
                    </tr>
                    
                  </thead>
                  <tbody>
                      @foreach ($data as $row)
                    <tr>
                      <td>{{ $row->customer}}</td>
                      <td>{{ $row->vessel}}</td>
                      <td>{{ $row->scopejob}}</td>
                      <td>{{ $row->pergantian_sparepart}}</td>
                      <td>{{ $row->teknisi}}</td>
                      <td>{{ $row->tahun}}</td>
                      <td>{{ $row->keterangan}}</td>
                      <td>{{ $row->files->count()}}</td>
                      <td>
                        <a href={{route('data.files',$row->id)}} type="button" class="btn btn-info m-1">FILE</a>
                        <a href="/show_rekap/{{ $row->id}}" class="btn btn-warning m-1">EDIT</a>
                        <a href="#" class="btn btn-danger delete m-1" data-id="{{ $row->id}}" data-customer="{{ $row->customer}}" >DELETE</a>
                        
                      </td>
                    </tr>
                    @endforeach
                  </tbody>
                </table>
                  </tbody>
                </table>
                {{ $data->links() }}
          </div>      
      </div>
  </div>
    @endsection
          <script>
            $('.delete').click(function(){
              var rekapid = $(this).attr('data-id');
              var customer = $(this).attr('data-customer');
              swal({
                title: "Yakin",
                text: "Kamu akan menghapus data dengan nama "+customer+" ",
                icon: "warning",
                buttons: true,
                dangerMode: true,
              })
              .then((willDelete) => {
                if (willDelete) {
                window.location = "/delete_rekap/"+rekapid+" "
                  swal("Data Berhasil Terhapus", {
                    icon: "success",
                  });
                } else {
                  swal("File Anda Aman");
                }
              });
            });
          </script>

How ti fix it ?

Advertisement

Answer

If you want to work with other developers in the future, make sure to work to common coding standards, such as using upper camel case for classes. Also consider the correct indentation.

Anyway, you don’t actually delete the database record when deleting files

    $files = file_rekap::where("rekap_id", $data->id)->get();
    foreach($files as $file) {
      if (File::exists("rekap_file/".$file->file)) {
        File::delete("rekap_file/".$file->file);
      }
      $file->delete();
    }

This isn’t the optimum case as a database query is executed for every loop over the files collection.

Better is to perform a separate delete

public function delete_rekap($id){
    $data = rekap::findOrfail($id);

    $files = file_rekap::where("rekap_id", $data->id)->get();
    foreach($files as $file){
      if (File::exists("rekap_file/".$file->file)) {
        File::delete("rekap_file/".$file->file);
      }
    }
    
    file_rekap::where("rekap_id", $data->id)->delete();
    
    $data->delete();
    
    return redirect()->route('rekap')->with('success','Data berhasil dihapus');
}

if you have a relationship set up, you can also delete via that

$data->file_recap()->delete();

make sure your data is not important when experimenting with delete

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement