Viewing file: PostRepository.php (4.83 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Repositories\Back;
use App\{ Models\Post, Helpers\ImageHelper }; use Illuminate\Support\Str;
class PostRepository {
/** * Store post. * * @param \App\Http\Requests\ImageStoreRequest $request * @return void */
public function store($request) { $input = $request->all(); $input['slug'] = Str::slug($request->title); if($request->has('tags')){ $input['tags'] = str_replace(["value", "{", "}", "[","]",":","\""], '', $request->tags); } if($request->photo){ $input['photo'] = $this->storeImageData($request); } Post::create($input); }
/** * Update post. * * @param \App\Http\Requests\ImageUpdateRequest $request * @return void */
public function update($post, $request) { $input = $request->all(); // dd($input); $input['slug'] = Str::slug($request->title); if($request->has('tags')){ $input['tags'] = str_replace(["value", "{", "}", "[","]",":","\""], '', $request->tags); } // dd($request->photo); if($request->photo){ $input['photo'] = $this->UpdateImageData($request,$post); } $post->update($input); }
// public function storeImageData($request) // { // $storeData = []; // if ($photos = $request->file('photo')) { // foreach($photos as $key => $photo){ // $storeData[$key] = ImageHelper::handleUploadedImage($photo,'assets/images'); // } // } // return $storeData; // } public function storeImageData($request) { if ($request->hasFile('photo')) { return ImageHelper::handleUploadedImage($request->file('photo'), 'assets/images'); }
return null; // Return null if no image is uploaded }
// public function updateImageData($request, $post) // { // $storeData = $post->photo ? json_decode($post->photo, true) : []; // if ($photos = $request->file('photo')) { // foreach ($photos as $photo) { // if ($photo->isValid()) { // array_push($storeData, ImageHelper::handleUploadedImage($photo, 'assets/images')); // } else { // return back()->withErrors(['photo' => 'Invalid image file.']); // } // } // } // return $storeData; // } public function updateImageData($request, $post) { // If the post already has an image, keep it unless a new one is uploaded $storeData = $post->photo ? $post->photo : null;
if ($request->hasFile('photo')) { $photo = $request->file('photo'); if ($photo->isValid()) { $storeData = ImageHelper::handleUploadedImage($photo, 'assets/images'); } else { return back()->withErrors(['photo' => 'Invalid image file.']); } }
return $storeData; }
/** * Delete post. * * @param int $id * @return \Illuminate\Http\Response */
// public function delete($post) // { // $images = json_decode($post->photo,true); // foreach($images as $image){ // if (file_exists(base_path('../').'assets/images/'.$image)) { // unlink(base_path('../').'assets/images/'.$image); // } // } // $post->delete(); // } public function delete($post) { if (!empty($post->photo)) { $imagePath = base_path('../') . 'assets/images/' . $post->photo; if (file_exists($imagePath)) { unlink($imagePath); } } $post->delete(); } /** * Delete post. * * @param int $id * @return \Illuminate\Http\Response */
// public function photoDelete($key,$id) // { // $post = Post::findOrFail($id); // $photos = json_decode($post->photo,true); // $delete_photo = $photos[$key]; // if (file_exists(base_path('../').'assets/images/'.$delete_photo)) { // unlink(base_path('../').'assets/images/'.$delete_photo); // } // unset($photos[$key]); // $new_photos = json_encode($photos,true); // $post->update(['photo'=> $new_photos]); // } public function photoDelete($id) { $post = Post::findOrFail($id); if (!empty($post->photo)) { $imagePath = base_path('../') . 'assets/images/' . $post->photo; if (file_exists($imagePath)) { unlink($imagePath); } // Remove the photo reference from the database $post->update(['photo' => null]); } } }
|