public static function boot() { parent::boot(); Media::deleting(function ($medium) { //Delete the actual file from disk \File::delete(storage_path() . '/app' . $medium->path); }); }
/** * Update the specified resource in storage. * * @param UpdatePostRequest $request * @param int $id * @return Response */ public function update(UpdatePostRequest $request, $id) { $post = $this->post->findOrFail($id); $checkboxes = ['is_active']; foreach ($checkboxes as $chk) { if (!$request->has($chk)) { $request->merge([$chk => 0]); } } if ($post->update($request->except('_token', 'media'))) { if ($request->has('media')) { // Delete old ones except the ones kept ;) $old_media = $post->media()->whereNotIn('path', $request->media)->get(); foreach ($old_media as $medium) { $medium->delete(); } // and save new ones foreach ($request->media as $index => $req_medium) { $tmp = Media::wherePath($req_medium)->first(); $tmp->ordr = $index; $tmp->save(); $media[] = $tmp; } $post->media()->saveMany($media); } $post->categories()->sync($request->categories); $msg = 'Post was updated successfully'; $msg_type = 'success'; } else { $msg = 'Something went wrong'; $msg_type = 'danger'; } return redirect('admin/posts')->with('msg', $msg)->with('msg_type', $msg_type); }