【问题标题】:Handle arrays in laravel在 laravel 中处理数组
【发布时间】:2012-12-26 05:04:48
【问题描述】:

我正在学习 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 中的数组。

【问题讨论】:

    标签: php arrays foreach views laravel


    【解决方案1】:

    查看如何使用 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;
    

    小便小便。

    【讨论】:

    • 这很酷,但是,我的帖子表有 2 个 ID 列,第一列用于 AI,其他与它们相关的线程。我应该将 thread_id 更改为 id 吗?
    • 每张表应该只有一个主键,最好叫“id”。不确定程序的其余逻辑如何与这些模型相关联,但是,如果您希望将该主键称为“id”以外的其他名称,则可以在 Post 类中设置 public $key = "thread_id";例如。
    • 谢谢,而且我不明白的是 find() 函数只得到一个帖子的帖子,但帖子有 10 个帖子。
    • 我重写了答案......希望这对你来说更好。
    【解决方案2】:

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

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

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2017-03-05
      • 2014-10-07
      • 2012-09-23
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多