一尘不染

如何删除我的数据库 Laravel 中的文件

php

如何删除数据库中的文件,因为当我单击删除时,文件夹中的数据被删除,但file_rekap表中的数据不想被删除,但rekap表中的数据被删除

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'));
}

型号rekapfile_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);
    }
}

和我的脚本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>

如何修复它?


阅读 115

收藏
2022-07-23

共1个答案

一尘不染

如果您将来想与其他开发人员合作,请确保使用通用编码标准,例如对类使用大写驼峰式。还要考虑正确的缩进。

无论如何,您在删除文件时实际上并没有删除数据库记录

    $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();
    }

这不是最佳情况,因为对文件集合的每个循环都执行数据库查询。

更好的是执行单独的删除

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');
}

如果您建立了关系,您也可以通过该关系删除

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

在尝试删除时确保您的数据不重要

2022-07-23