Level 60
foreach($projectData->projectImages // try without parentheses
1 like
I have project data in table named "projects" where project images in separate table named "project_images" and my main issue here is that the images uploaded in "uploads/projects" folder does not deleted not sure why...?!
Blade.php
<div class="remove">
<button class="btn btn-sm btn-danger remove-item-btn" onclick="deleteProject({{ $project }})">Delete</button>
</div>
<!-- Remove Modal -->
<div class="modal fade zoomIn" id="deleteProjectModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="btn-close"></button>
</div>
<form id="DeleteForm">
@csrf
<div class="modal-body">
<input type="hidden" name="projectid_dfield" id="projectid_dfield">
<div class="mt-2 text-center">
<lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop"
colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon>
<div class="mt-4 pt-2 fs-15 mx-4 mx-sm-5">
<h4>Are you Sure You want to Delete ?</h4>
<p class="text-muted mx-4 mb-0 fw-bold" id="deletedName"></p>
</div>
</div>
<div class="d-flex gap-2 justify-content-center mt-4 mb-2">
<button type="button" class="btn w-sm btn-light" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn w-sm btn-danger " id="delete-record">Yes, Delete It!</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!--end remove modal -->
<script type="text/javascript">
function deleteProject(e) {
var projectId = e.id;
var projectName = e.project_title;
$("#projectid_dfield").val(projectId);
$("#deletedName").text(projectName);
$('#deleteProjectModal').modal('show');
}
$('body').on('submit', '#DeleteForm', function(e){
let id = $('#projectid_dfield').val();
let url = window.location.origin;
let path = url + '/user/projects/delete' + '/' + id;
axios.get(path)
.then( res => {
/*$('#deleteUserModal').modal('toggle');
location.reload();*/
console.log(res)
})
.catch( err => console.log(err));
})
</script>
web.php
Route::controller(ProjectsController::class)->middleware(['auth', 'user'])->prefix('user')->group( function () {
Route::get('/projects/delete/{id}', 'DeleteProjectUser')->name('DeleteProjectUser');
});
Controller.php
public function DeleteProjectUser(Request $request, $id) {
$projectData = Projects::find($id);
if($projectData->projectImages()) {
foreach($projectData->projectImages() as $image){
$image_path = public_path('uploads\projects').'\'.$image->image;
if(File::exists($image_path)){
File::delete($image_path);
}
$image->delete();
}
}
$projectData->delete();
return redirect()->back()->with('success', 'تم حذف المشروع بنجاح');
}
Projects modal
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\UsersList;
use App\Models\ProjectImages;
class Projects extends Model
{
use SoftDeletes;
protected $fillable = [
'project_title',
'project_shorts',
];
public function projectImages(){
return $this->hasMany(ProjectImages::class, 'projects_id', 'id');
}
}
ProjectImages modal
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Projects;
class ProjectImages extends Model
{
use SoftDeletes;
protected $fillable = [
'projects_id',
'image',
];
public function projectInfo() {
return $this->hasOne(Projects::class, 'id', 'projects_id');
}
}
foreach($projectData->projectImages // try without parentheses
Please or to participate in this conversation.