2013-01-11 86 views
0

我正在学习laravel并试图用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中的数组。

回答

3

查一查如何建立模型和它们之间的关系用雄辩的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; 

容易peezy。

+0

天啊!我会试试这个,谢谢! –

+0

多数民众赞成酷,但是,我的帖子表有2个ID列一个AI和其他相关的线程。我应该改变thread_id ID? –

+0

每个表只能有一个主键,最好称它为“id”。不知道程序的其余部分如何与这些模型联系起来,但是,如果您希望将主键称为“id”以外的其他名称,那么在Post类中您可以设置public key =“thread_id”;例如。 –

1

随着雄辩,你可以用多种方式做到这一点。

首先,您可以在您的发布模型中添加一个方法来检索相关数据。这种方法会为每个帖子创建一个新的查询,所以它通常不是最好的选择。

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

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

// Example from Laravel.com 

foreach (Book::with('author')->get() as $book) 
{ 
    echo $book->author->name; 
}