【发布时间】:2015-08-04 14:24:11
【问题描述】:
我正在尝试使用 Laravel 5 构建一个简单的 CMS,但我被阻止了:
类别标题没问题,但在同一个(复制论坛)内?为什么?我的代码:
@extends('layouts.main')
@section('content')
@foreach($categories as $category)
<div class="panel panel-default">
<div class="panel-heading">{{ $category->title }}</div>
<div class="panel-body">
<table class="table">
<tbody>
@foreach($forums as $forum)
<tr>
<th scope="row">{{ $forum->id }}</th>
<td><a href="{{ generateForumURL($forum->seo_name, $forum->id) }}">{{ $forum->name }}</a></td>
<td>{{ $forum->topics }}</td>
<td>{{ $forum->posts }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endforeach
@stop
在路线中:
Route::get('', function()
{
$forums = DB::table('forums')
->select()
->get();
$categories = DB::table('categories')
->select()
->get();
return View::make('home', compact('forums', 'categories'));
});
在 PhpMyAdmin 中:
我知道我没有做某事,但我不知道是什么,我是 Laravel 的新手。 P.S 我的英语不好,抱歉我的语言不好:) 提前非常感谢;)
很快:我不想在in_category 行中写的那个类别中显示论坛。谢谢。
【问题讨论】:
-
我不明白你在问什么。
-
@Jamesking56 正如你现在看到的,在两个类别中打印相同的论坛,但我不想在一个类别的论坛中打印,在其他类别的其他论坛中。我在 PhpMyAdmin 中有表格:
in_category每个类别都有唯一的 id。所以我不想通过 id 打印 x 类别中的 x 论坛 -
我怀疑你是否理解我:D
-
我现在了解你了。您需要定义类别与类别内的论坛之间的关系。您可以通过查询构建器使用连接来执行此操作,或者我建议您定义eloquent models 和define an eloquent relationship,因为这将允许您执行
$category->forums以获取给定类别中的论坛。 -
@Jamesking56 非常感谢。也许你能试着为我写代码吗? :((
标签: php laravel content-management-system laravel-5