Alodon's avatar

Delete post image

Hi, I have two controllers one is to handle images of my post and another one is to handle all other information. Now I want to delete images while updating or deleting the post. Is there is any way to send two different request to both controllers for deleting images?

This is my post controller;

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  int $id
 * @return void
 */
public function update(Request $request, $id)
{
    //

    $this->validate($request, [

        'title'=> 'required',
        'category_id' => 'required',
        'featured' => 'image',
        'body' => 'required'

    ]);

    $post= Post::find($id);
    
    $featured = $request->file('featured');


    if ($request->hasFile('featured')){
    
        $featured_new_name = time().$featured->getClientOriginalName();

        $postImage = Image::make($featured)->resize(1600,1066)->save('uploads/posts/'.$featured_new_name);
    }

    
    $post->title = $request->title;

    $post->content = $request->body;

    $post->category_id= $request->category_id;

    $pathToDelete = 'C:\wamp\www\tricky\uploads\posts\'.basename($post->featured);
    
    unlink($pathToDelete);

    $post->featured = 'uploads/posts/'. $featured_new_name;

    $post->slug = Str::slug($request->title);

    $post->save();

    $post->tags()->sync($request->tags);

    $post->locations()->sync($request->locations);


    session()->flash('message', 'The Blog Post Has Been Successfully Edited.');
    Session::flash('type', 'success');
    Session::flash('title', 'Edited Successful');


    return redirect()->route('user.posts.index');



}

delete:

public function destroy($id)
{

    $post= Post::find($id);
    $post->tags()->detach();
    $post->delete();
    return redirect()->back();
}

This is my image controller:

public function upload(Request $request) { if($request->hasFile('upload')) { //get filename with extension $filenamewithextension = $request->file('upload')->getClientOriginalName();

        //get filename without extension
        $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
  
        //get file extension
        $extension = $request->file('upload')->getClientOriginalExtension();
  
        //filename to store
        $filenametostore = $filename.'_'.time().'.'.$extension;
  
        //Upload File
        $request->file('upload')->storeAs('public/uploads', $filenametostore);

        $CKEditorFuncNum = $request->input('CKEditorFuncNum');
        $url = asset('1traveller/storage/app/public/uploads/'.$filenametostore); 
        $msg = 'Image successfully uploaded'; 
        $re = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
         
        // Render HTML output 
        @header('Content-type: text/html; charset=utf-8'); 
        echo $re;
    }
}
0 likes
2 replies
Ashraam's avatar

You should take a look to model observer ( https://laravel.com/docs/7.x/eloquent#observers )

The idea woud be

You create an observer for your post model and in the deleting method you could do something like

function deleting(Post $post) {
   foreach($post->photos as $photo) {
      unlink($photo->path);
      $photo->delete();
   }
}

this is just a silly example you could improve this a tons

Alodon's avatar

Thanks. Is there is any way to send two request route?

Please or to participate in this conversation.