删除属于帖子的文件?

时间:2017-07-06 12:18:45

标签: laravel laravel-5

我有两个名为' posts'和照片'照片'表存储' post'的图像。一篇文章可以有很多照片,所以关系是一对多的。我已经设法创建了商店方法,我已经创建了一个视图来显示包含照片的帖子。以下是功能:

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

节目视图:

<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 -->

我希望能够从节目视图中删除图像,该怎么做?我需要为它创建一个函数吗?如果是这样,那么该功能将如何? 这就是全部,谢谢,对不起要求太多了

1 个答案:

答案 0 :(得分:0)

首先,通过添加以下内容编辑视图中的delete form

...
<input type="hidden" name="image_id" value="{{ $images->id }}" /> // Assuming that photo id is in your $images object
...

然后,您可以从POST创建与image/delete功能相关联的delete路由controller,以删除预期的图片:

public function delete() {
    $image_id = $request->only('image_id');

    $image = Photo::where('id', '=', $image_id)->delete(); // Assuming that you have a Photo model

    return view('dashboard/posts/show');
}