2014-11-03 36 views
0

我工作過的一項基本制度,其中博客檢索其他數據庫表中的圖像,但它顯示的職位而不是照片 這裏是我的控制器:檢索行laravel

$bloginfo=bloginfo::where('blog_name','=',$name)->first(); 
    $blogposts=pageposts::where('p_id','=',$bloginfo->id)->orderBy('id','desc')->get(); 
    foreach($blogposts as $posts){ 
     $photoscount=pagephotos::where('b_id','=',$posts->id)->count(); 
     $blogphotos=pagephotos::where('b_id','=',$posts->id)->get(); 

    } 
    return View::make('myblog') 
    ->with('bloginfo',$bloginfo) 
    ->with('blogposts',$blogposts) 
    ->with('blogphotos',$blogphotos) 
    ->with('photoscount',$photoscount); 

,這裏是我的看法:

@foreach($blogposts as $posts) 
        <div class="panel panel-default">  
         <div class="panel-body"> 
          @if($photoscount==0) 
           {{ null }} 
          @elseif($photoscount==1) 
          //a bunch of code here 
           @else 

           //a bunch of code here 
           @endif 
          <h4><a href="">{{ $posts->title }}</a></h4> 
          <p>{{ $posts->description }}</p> 

         </div> 
        </div> 
        @endforeach 
+0

您是否在控制器中獲取照片?你用print_r檢查過嗎? – 2014-11-03 10:49:37

+0

我只根據帖子檢索照片名稱。意思是當我發佈標題描述和一個以上的圖像在帖子中,所以thr標題和drscription將被插入到表格中,而照片名稱將會被張貼在具有相同的博客ID(b_id)的其他表格中,現在我想要回顧圖像與博客。 – 2014-11-03 10:55:51

+0

您是否獲取圖片網址?或圖像名稱?如果圖像名稱是這些圖像保存在同一臺服務器上。或者你有一些訪問它們的路徑。 – 2014-11-03 11:01:01

回答

1

可能我建議你正在尋找關係,這也可能是你的答案。

您可以在一個查詢中加載所有數據。

bloginfo::with('posts', 'posts.images')->where('blog_name','=',$name)->first(); 

這將載入與其相關的所有信息(即帖子)及其圖像的博客帖子。看這裏設置它們。 HERE

由於您當前的問題在您的for循環中,因此您將覆蓋您設置n次的變量,即根據帖子的數量。所以你可能會有不同的結果。