Making dynamic pages from DB with Laravel

若如初见. 提交于 2019-12-13 02:54:53

问题


At the moment I'm getting all the rows from my table, and display every category on the page. Now I want to make something new but I'm a bit stuck after trying a few things. So now I have a link called 'photos' and when that's being clicked all the photos are being shown. Now I want to have a submenu so that I can only see the photos of a certain category. This is how a table can look like

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

When navigating to www.mysite.com/photos, then this displays all my photos regardless of the category. Now I want to add the functionality when going to www.mysite.com/photos/portret that I only see photos from the category portret.

I was able to create the links dynamically and they go to the correct URL (www.mysite.com/photos), but the page is empty. So I don't know what is going wrong. Routing?

I'll post what I have tried and what I currently have below.

Before the navigation was static, but now that I want it dynamic I added the 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();
}

And it's corresponding view is nav.blade that contains this (This prints out my dynamic links)

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

Then in my route I have so I can navigate to the dynamic pages

Route::get('photos/{theme}', array('as' => '{theme}', 'uses' => 'PhotosController@show'));

Then here in my PhotosController I have

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

And in my view 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

So I don't see or understand what I'm doing wrong. Also when going to the page www.mysite.com/photos/portret, the dynamic links don't appear anymore while this should be as it's only in the nav.blade that's being included in my template. Can someone help me please?

EDIT: Most of my work here is due an other Q/A I found on SO and that's this Laravel Creating Dynamic Routes to controllers from Mysql database


回答1:


Your code looks almost good to me, but you forgetting to get your photos from the database:

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

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


来源:https://stackoverflow.com/questions/25672054/making-dynamic-pages-from-db-with-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!