我正在学习 laravel 并尝试用 laravel 创建一个社区。我卡在某个部分,需要问你们。
现在我正在尝试向读者展示帖子。
这就是我到目前为止所完成的
使用此代码:
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 中的数组。