0

我正在学习 laravel 并尝试用 laravel 创建一个社区。我卡在某个部分,需要问你们。

现在我正在尝试向读者展示帖子。

这就是我到目前为止所完成的 http://i.imgur.com/lc5gX.jpg

使用此代码:

public $restful = true;
public function get_index($title = '')
{
    //Do we have an arguement?
        if(empty($title))
        {return Redirect::to('dashboard');}

    $view = View::make('entry');
    $query = DB::table('threads')->where('title', '=', $title)->first();

    //Do we have this thread?
        if(!$query)
        {return Redirect::to('entry/suggest');}
    //We have? so lets rock it!
    $view->title = $query->title; //we get the title.

    $thread_id = $query->id;

    //getting posts.

    $posts = DB::table('posts')->where('thread_id', '=', $thread_id)->get();


    $view->posts = $posts;

  return $view;
}

观点是:

<div id="entrybox">
    <h2>{{$title}}</h2>
@foreach($posts as $post)
    <p class="entry"><span class="entrynumber">1</span><span class="entry_text">{{$post->post_entry}}</span></p>
    <p align="right" class="edate"><span class="entry_user">{post_row.POST_USERNAME}</span> <span class="entry_date">{post_row.DATE}</span></p>
@endforeach
</div>

但困难的部分(对我来说)是将帖子与发布者的用户名或其他用户信息(如电子邮件)集成以获得中等权限。但是,嘿在“用户”表中。

假设 $posts 变量包含一个线程的 10 个帖子的数据 *一个帖子有 thread_id 、poster_userid、post、posted_date。

如何从“用户”表中获取用户名并将其发送到我的视图?我确定在从数据库中选择帖子后我需要使用 foreach 我只是不知道如何处理 foreach 中的数组。

4

2 回答 2

3

查看如何使用 Eloquent ORM 设置模型及其关系:http: //laravel.com/docs/database/eloquent

具体http://laravel.com/docs/database/eloquent#relationships

你应该有三个模型,线程,帖子和用户(将poster_userid更改为user_id......或者你可以保留它,但我不建议这样做......只是保持简单。你也可以使用author_id甚至poster_id,如果那样的话漂浮你的船..只要确保用你选择的东西改变'user_id')

//application/models/User.php

class User extends Eloquent {

 public function posts()
 {
      return $this->has_many('Post', 'user_id');
 }
}

//application/models/Post.php
class Post extends Eloquent {

 public function author()
 {
    return $this->belongs_to('User', 'user_id');
 }

 public function thread()
 {
    return $this->belongs_to('Thread', 'thread_id');
 }
}

 //application/models/Thread.php
    class Thread extends Eloquent {

     public function posts()
     {
          return $this->has_many('Post', 'thread_id');
     }

    }

而不是

$query = DB::table('threads')->where('title', '=', $title)->first();

//Do we have this thread?
    if(!$query)
    {return Redirect::to('entry/suggest');}
//We have? so lets rock it!
$view->title = $query->title; //we get the title.

$thread_id = $query->id;

//getting posts.

$posts = DB::table('posts')->where('thread_id', '=', $thread_id)->get();

我只想把

$thread = Thread::where('title', '=', $title)->first();

if(!$query)
    {return Redirect::to('entry/suggest');}

$posts = $thread->posts()->get();
//or if you want to eager load the author of the posts:
//$posts = $thread->posts()->with('author')->get();

然后在您的视图中,您可以通过以下方式查找用户名:

$post->author->username;

容易小便。

于 2013-01-11T15:42:57.617 回答
1

使用 Eloquent,您可以通过多种方式做到这一点。

首先,您可以向您的帖子模型添加一个检索相关数据的方法。这种方法将为每个帖子创建一个新查询,因此通常不是最佳选择。

class Post extends Eloquent
{
    public function user()
    {
        return $this->has_one('User');
    }
}

其次,您可以修改初始查询以加入所需数据并避免创建撤消查询。称为“急切加载”。

// Example from Laravel.com

foreach (Book::with('author')->get() as $book)
{
    echo $book->author->name;
}
于 2013-01-11T15:44:52.750 回答