2017-07-06 91 views
1

我有兩張名爲'posts'和'photos'的表格。 「照片」表格存儲「帖子」的圖片。一篇文章可以有很多照片,所以這種關係是一對多的。我已經設法創建商店方法,我已經創建了視圖以顯示其照片的帖子。下面是函數:刪除屬於帖子的文件?

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

我希望能夠刪除從展會查看圖像,如何做到這一點?我需要爲它創建一個函數嗎?如果是這樣,那麼函數將如何? 這一切和感謝,對不起,問太多

+0

您的PostsController中是否有銷燬方法? – OuailB

回答

0

首先,通過添加編輯視圖您delete form

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

然後,你可以從你的controller創建關聯到delete功能POST路線image/delete刪除預期的圖像:

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'); 
}