Php laravel中的句柄数组

Php laravel中的句柄数组,php,arrays,foreach,views,laravel,Php,Arrays,Foreach,Views,Laravel,我正在学习laravel,并试图与laravel建立一个社区。我在某个地方卡住了,需要问问你们 现在我正试图向读者展示帖子 这就是我迄今为止所取得的成就 使用此代码: public $restful = true; public function get_index($title = '') { //Do we have an arguement? if(empty($title)) {return Redirect::to('dashboard');}

我正在学习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>

{{$title}}
@foreach($posts作为$post)

1{{$post->post\u entry}

{post_row.post_USERNAME}{post_row.DATE}

@endforeach
但(对我来说)最困难的部分是将帖子与海报的用户名或其他用户信息(如电子邮件)集成,以获得中等权限。然而,他们在“用户”表中

假设$posts变量为一个线程保存10个POST的数据 *帖子具有线程id、海报用户id、帖子、发布日期


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

查找如何使用雄辩的ORM设置模型及其关系:

具体地

你应该有三个模型,线程、帖子和用户(将poster\u userid更改为User\u id…或者你可以保留它,但我不建议…保持简单。你也可以使用author\u id,或者甚至可以使用poster\u id,如果你的船漂浮在水面上的话。只要确保用你选择的内容更改“User\u 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;

易怒。

查看如何使用雄辩的ORM建立模型及其关系:

具体地

你应该有三个模型,线程、帖子和用户(将poster\u userid更改为User\u id…或者你可以保留它,但我不建议…保持简单。你也可以使用author\u id,或者甚至可以使用poster\u id,如果你的船漂浮在水面上的话。只要确保用你选择的内容更改“User\u 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;

容易生气。

有了雄辩,你可以用多种方式来做到这一点

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

class Post extends Eloquent
{
    public function user()
    {
        return $this->has_one('User');
    }
}
其次,您可以修改初始查询以加入所需的数据,并避免创建撤消查询。我们称之为“”


有了雄辩,你可以用多种方式做到这一点

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

class Post extends Eloquent
{
    public function user()
    {
        return $this->has_one('User');
    }
}
其次,您可以修改初始查询以加入所需的数据,并避免创建撤消查询。我们称之为“”


这很酷,但是,我的帖子表有两个ID列,一个用于AI,另一个用于它们相关的线程。我是否必须将线程id更改为id?每个表应该只有一个主键,最好将其称为“id”。不确定程序的其余逻辑是如何与这些模型联系在一起的,但是,如果希望主键被调用为“id”以外的其他名称,那么在Post类中可以设置public$key=“thread\u id”;例如,谢谢,我不明白的是find()函数只得到一个线程的一个帖子,但是线程有10个帖子。我重写了答案。。。希望这对你更好。这很酷,但是我的帖子表有两个ID列,一个是AI列,另一个是与它们相关的线程列。我是否必须将线程id更改为id?每个表应该只有一个主键,最好将其称为“id”。不确定程序的其余逻辑是如何与这些模型联系在一起的,但是,如果希望主键被调用为“id”以外的其他名称,那么在Post类中可以设置public$key=“thread\u id”;例如,谢谢,我不明白的是find()函数只得到一个线程的一个帖子,但是线程有10个帖子。我重写了答案。。。希望这对你更好。