2014-09-04 94 views
0

目前,我從我的表中獲取所有行,並在頁面上顯示每個類別。現在我想創造新的東西,但在嘗試幾件事情後我有點卡住了。 所以現在我有一個名爲'照片'的鏈接,當點擊時,所有的照片都顯示出來。現在我想要一個子菜單,以便我只能看到某個類別的照片。這是一臺能看怎麼樣使用Laravel從數據庫製作動態頁面

pk imgUrl category 
-------------------- 
1 ...  portret 
2 ...  nature 
3 ...  portret 
4 ...  cars 

當導航到www.mysite.com/photos,那麼這個類別無關的顯示我的所有照片。 現在我想添加功能,當去www.mysite.com/photos/portret,我只看到類別portret的照片。

我能夠動態創建鏈接,並轉到正確的URL(www.mysite.com/photos),但頁面爲空。所以我不知道發生了什麼問題。路由?

我會發布我已經嘗試過的以及我目前在下面的內容。

導航是靜態的,但現在,我想它的動態我加入了NavController

public function index() 
{ 
    // 
    //return Photo::all(); 
    $title = "Photo"; 
    $photos = Photo::orderBy('category')->get(); 
    return View::make('photo')->with('title', $title)->with('photos', $photos); 

    //QUERY - Eloquent 
    //return Photo::all(); 
} 

和它對應的觀點是nav.blade包含此(這打印出我的動態鏈接)

<?php 
    $category = ""; 
?> 
<ul> 
    @foreach($photos as $photo) 
     @if($category != $photo->Category) 
      <li><a href="{{ $photo->Category }}"> {{ $photo->Category }} </a></li> 
      <?php $category = $photo->Category; ?> 
     @endif 
    @endforeach 
</ul> 

然後在我的路線我有這樣我就可以導航到動態頁面

Route::get('photos/{theme}', array('as' => '{theme}', 'uses' => '[email protected]')); 

那麼這裏我PhotosController

public function show($theme){ 
     $photos = Photo::where('category', $theme); 
     return View::make('photo')->with('title', $theme)->with('photos', $photos); 
    } 

在我看來photos.blade

<?php 
     $category = ""; 
    ?> 
    @foreach($photos as $photo) 
     <a href="#myModal" class="linkWrapper"> {{ HTML::image("img/$photo->Link", "$photo->Title", array("class"=>"thumbnail thumbEffect")) }} </a> 
    @endforeach 

所以我沒有看到或理解我」做錯了。另外,當進入www.mysite.com/photos/portret頁面時,動態鏈接不再出現,而這應該是因爲它只在我的模板中包含的nav.blade中。 有人可以幫我嗎?

編輯:大多數我在這裏的工作是由於一個其他Q/AI SO發現,這就是這個Laravel Creating Dynamic Routes to controllers from Mysql database

回答

1

你的代碼看起來幾乎是對我好,但你忘了從數據庫中獲取您的照片:

public function show($theme) 
{ 
    $photos = Photo::where('category', $theme)->get(); /// here 

    return View::make('photo')->with('title', $theme)->with('photos', $photos); 
} 
+0

謝謝!如果你一遍又一遍地查看相同的代碼,很容易錯過一些東西。添加get()確實是解決方案。 – mXX 2014-09-07 08:18:53