dqlhsm9820
2013-01-11 15:20
浏览 60
已采纳

在laravel中处理数组

I am learning laravel and trying to create a community with laravel. I stuck at some part and need to ask you guys.

Right now I am trying to displaying posts to reader.

This is what i accomplish so far http://i.imgur.com/lc5gX.jpg

with this code :

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

and the view is :

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

But the hard part(for me) is integrate posts with poster's username or other user info like e-mail for moderate privileges. However hey are in 'users' table.

Lets say $posts variable holds data of 10 posts for a thread *a post has thread_id , poster_userid, post, posted_date.

How can i grab username from 'users' table and send it to my view? I am sure i need to use foreach after selecting posts from database I just dont know how to handle arrays in foreach.

图片转代码服务由CSDN问答提供 功能建议

我正在学习laravel并试图用laravel创建一个社区。 我坚持某些部分需要问你们。

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

这就是我所完成的 到目前为止 \ n

使用以下代码:

  public $ restful = true; 
公共函数get_index($ title ='')
 {
 //我们有没有 arguement?
 if(empty($ title))
 {return Redirect :: to('dashboard');} 
 
 $ view = View :: make('entry'); 
 $ query = DB  :: table('threads') - &gt; where('title','=',$ title) - &gt; first(); 
 
 //我们有这个帖子吗?
 if(!$ query)  )
 {return Redirect :: to('entry / suggest');} 
 //我们有? 所以我们摇滚吧!
 $ view-&gt; title = $ query-&gt; title;  //我们得到了标题。
 
 $ thread_id = $ query-&gt; id; 
 
 //获取帖子。
 
 $ posts = DB :: table('posts') - &gt; where  ('thread_id','=',$ thread_id) - &gt; get(); 
 
 
 $ view-&gt; posts = $ posts; 
 
返回$ view; 
} 
   
 
 

,视图为:

 &lt; div id =“entrybox”&gt; 
&lt; h2&gt; {{  $ title}}&lt; / h2&gt; 
 @ foreach($ posts as $ post)
&lt; p class =“entry”&gt;&lt; span class =“entrynumber”&gt; 1&lt; / span&gt;&lt; span  class =“entry_text”&gt; {{$ post-&gt; post_entry}}&lt; / span&gt;&lt; / p&gt; 
&lt; p align =“right”class =“edate”&gt;&lt; span class =“  entry_user“&GT; {post_row.POST_USERNAME}&LT; /跨度&GT;  &lt; span class =“entry_date”&gt; {post_row.DATE}&lt; / span&gt;&lt; / p&gt; 
 @ endforeach 
&lt; / div&gt; 
   
 
 < 但是,对我来说,困难的部分是将帖子与海报的用户名或其他用户信息(如电子邮件)整合在一起,以获得适度的特权。 然而,他们在'用户'表中。 
 
 

让我们说$ posts变量保存一个帖子的10个帖子的数据 *帖子有thread_id,poster_userid,post,posted_date。

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douyu3145 2013-01-11 15:42
    最佳回答

    Look up how to set up models and their relationships using Eloquent ORM: http://laravel.com/docs/database/eloquent

    Specifically http://laravel.com/docs/database/eloquent#relationships

    You should have three models, Thread, Post and User (change poster_userid to user_id... or you can keep it but I wouldn't suggest it... just keep things simple. You could also use author_id or maybe even poster_id if that floats your boat.. just be sure to change 'user_id' out with what you choose)

    //application/models/User.php
    
    class User extends Eloquent {
    
     public function posts()
     {
          return $this->has_many('Post', 'user_id');
     }
    }
    

    and

    //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');
     }
    }
    

    and

     //application/models/Thread.php
        class Thread extends Eloquent {
    
         public function posts()
         {
              return $this->has_many('Post', 'thread_id');
         }
    
        }
    

    and instead of

    $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();
    

    I would just put

    $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();
    

    Then in your view you can look up the user's name by the following:

    $post->author->username;
    

    Easy peezy.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题