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 = [
'costumer',
'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 >
<div >
<input type="file" name="import_excel" required>
</div>
</div>
<div >
<button type="button" data-bs-dismiss="modal">Close</button>
<button type="submit" >Save changes</button>
</div>
</form>
</div>
</div>
</div>
<div >
<form action="/rekap" method="GET">
<div >
<input type="text" placeholder="Cari..." name="search">
<button type="submit">Search</button>
</div>
</div>
</form>
<table >
<thead >
<tr>
<th scope="col">Costumer</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->costumer}}</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" >FILE</a>
<a href="/show_rekap/{{ $row->id}}" >EDIT</a>
<a href="#" data-id="{{ $row->id}}" data-costumer="{{ $row->costumer}}" >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 costumer = $(this).attr('data-costumer');
swal({
title: "Yakin",
text: "Kamu akan menghapus data dengan nama " costumer " ",
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 ?
CodePudding user response:
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
CodePudding user response:
$status=unlink('data.txt');
// Delete a single file File::delete($filename);