【问题标题】:Laravel 5 CMS making?Laravel 5 CMS 制作?
【发布时间】: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 modelsdefine an eloquent relationship,因为这将允许您执行$category-&gt;forums 以获取给定类别中的论坛。
  • @Jamesking56 非常感谢。也许你能试着为我写代码吗? :((

标签: php laravel content-management-system laravel-5


【解决方案1】:

好的,这里有一个详细的答案。

在 Laravel 中,您可以使用 Eloquent 模型作为在应用程序中创建实体的一种方式。在这种情况下,我认为雄辩的模型将是最好的选择。

首先新建模型如下app/Category.php:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model {

    protected $table = "categories";

    public $timestamps = false;

    public function forums() {
        return $this->hasMany('App\Forum', 'in_category');
    }

}

然后创建另一个模型如下app/Forum.php:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Forum extends Model {

    protected $table = "forums";

    public $timestamps = false;

    public function category() {
        return $this->belongsTo('App\Category');
    }

}

完成此操作后,您现在可以如下修改路线:

<?php
use App\Category;

Route::get('', function()
{
    $categories = Category::all();

    return View::make('home', compact('categories'));
});

你的看法如下:

@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($category->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

这应该可以解决您的问题,方法是只显示该类别中该类别的论坛。

【讨论】:

  • Class 'Category' not found 也许我必须用 composer 写点什么?
  • 你把这两个模型类放在app/目录下了吗?
  • 我已经创建了 maually,创建了 .txt 文件,写入了文本,然后保存为 .php 文件
  • 是的,但它们在app 文件夹中吗?
  • 是的,它们在 app 文件夹中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 2015-07-20
  • 2013-08-13
相关资源
最近更新 更多