Latest

Thursday, July 6, 2017

Deleting files that belong to a post?

Asked by: PrestonGarvey22


I have two tables called 'posts' and 'photos'. The 'photos' table store the images for the 'post'. A post can have many photos so the relationship is one-to-many. I've managed to create the store method, I already create the view to show a post with its photos as well. Below is the function :

public function show($titleslug) {
        $post = Post::where('titleslug', '=', $titleslug)->first();
        $images = $post->photos;
        return view('dashboard/posts/show', compact('post','images', $post, $images));
}

The show view :

<div class="contentpanel">
    <div class="row blog-content">
        <div class="col-sm-12">
            <div class="panel panel-default panel-blog">
                <div class="panel-body">
                    <div class="panel-body">
                        <h3 class="blogsingle-title">{{$post->title}}</h3>
                        <ul class="blog-meta">
                            <li><i class="fa fa-clock-o"></i> Jan 02, 2014</li>
                        </ul>
                        <div class="mb20"></div>
                        {!! $post->content !!}
                    </div><!-- panel-body -->

                    <div class="mb20"></div>
                </div><!-- panel-body -->
            </div><!-- panel -->
        </div><!-- col-sm-8 -->
    </div><!-- row -->
    <div class="row blog-content">
        <div class="col-sm-12">
            <div class="panel panel-default panel-blog">
                <div class="panel-body">
                    <h3 class="blogsingle-title">This post featured images :</h3>
                    <div class="mb20"></div>
                    <?php foreach ($images as $i): ?>
                        <div class="col-xs-12 col-sm-4 col-md-3">
                            <div class="blog-item">
                                <a href="#" class="blog-img"><img src="{{asset('image/'. $i->image)}}" class="img-responsive" alt="" style="height: 150px;"/></a>
                                <div class="blog-details">
                                    <ul class="blog-meta">
                                        <li><a class="btn btn-sm btn-warning" title="Edit" href="#"><i class="fa fa-edit"></i></a></li>
                                        <form action="#" method="POST">
                                            <input type="hidden" name="_method" value="DELETE">
                                            <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                                            <li>
                                                <button type="submit" class="btn btn-sm btn-danger" title="Delete" onclick="return confirm('Are you sure deleting this photo ?');"><i class="fa fa-trash-o"></i></button>
                                            </li>
                                        </form>
                                    </ul>
                                </div>
                            </div><!-- blog-item -->
                        </div><!-- col-xs-6 -->
                    <?php endforeach; ?>
                </div>
                <div class="panel-body">
                    <div class="alert alert-info fade in">
                        <p>
                            <a href="#"><button type="button" class="btn btn-info">Add New Images</button></a>
                        </p>
                    </div>
                </div>
            </div><!-- panel -->
        </div>
    </div>
</div><!-- contentpanel -->

I want to be able to delete the images from the show view, how to do that ? Do I need to create a function to it ? If so, then how will the function looks like ? That's all and thanks, sorry for asking too much



Source

No comments:

Post a Comment

Adbox