lundi 29 janvier 2018

Deleting a comment and associated images

I'm trying to delete single user comments and the pictures related with them, by deleting them from both the database records and storage. Unfortunately I keep getting errors. Below is the code I have so far.

Tables: Comments: id, comment, user_id, person_id pictures: id, image, comment_id, user_id, person_id

Relationships: Comment:

public function pictures()
{
    return $this->hasMany('App\Picture', 'comments_id', 'id');
} 

pictures:

   public function comments()
    {
        return $this->belongsTo('App\Comment', 'comments_id');
    }

Controller:

public function destroy($id)
{
    $comment = Comment::find($id);
    Storage::delete($comment->pictures());

    $comment->delete();
    Session::flash('success', 'Your comment and pictures were deleted');

    return redirect()->route('home');
} 

Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire