使用Laravel从数据库制作动态页面 [英] Making dynamic pages from DB with Laravel

查看:62
本文介绍了使用Laravel从数据库制作动态页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我将从表中获取所有行,并在页面上显示每个类别.现在我想做些新的东西,但是尝试了几件事后我有点卡住了. 因此,现在我有一个名为照片"的链接,当单击该链接时,将显示所有照片.现在,我想有一个子菜单,这样我只能看到特定类别的照片.这是表格的样子

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

导航到www.mysite.com/photos时,这将显示我的所有照片,无论其类别是什么. 现在,我想在访问www.mysite.com/photos/portret时添加该功能,该功能仅显示类别portret中的照片.

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.

我能够动态创建链接,并且它们会转到正确的URL(www.mysite.com/photos),但是页面为空.所以我不知道出了什么问题.路由?

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.

在导航是静态的之前,但是现在我希望它是动态的,所以我添加了 NavController

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

与之对应的视图是 nav.blade ,其中包含该视图(这会打印出我的动态链接)

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

然后在我的 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中. 有人可以帮我吗?

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?

我在这里的大部分工作是由于在SO上找到了另一个Q/AI,而这就是

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

推荐答案

您的代码对我来说几乎不错,但是您却忘记了从数据库中获取照片:

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

这篇关于使用Laravel从数据库制作动态页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆