0

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

しかし、(私にとって)難しいのは、投稿を投稿者のユーザー名または電子メールなどの他のユーザー情報と統合して、中程度の特権を得ることです。ただし、「users」テーブルにいます。

$posts 変数がスレッドの 10 件の投稿のデータを保持しているとします *投稿には thread_id 、 poster_userid 、 post 、 posted_date があります。

「ユーザー」テーブルからユーザー名を取得してビューに送信するにはどうすればよいですか? データベースから投稿を選択した後、foreach を使用する必要があると確信しています。foreach で配列を処理する方法がわかりません。

4

2 に答える 2

3

Eloquent ORM を使用してモデルとその関係を設定する方法を調べてください: http://laravel.com/docs/database/eloquent

具体的にはhttp://laravel.com/docs/database/eloquent#relationships

Thread、Post、User の 3 つのモデルが必要です (poster_userid を user_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;

簡単にピージー。

于 2013-01-11T15:42:57.617 に答える
1

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;
}
于 2013-01-11T15:44:52.750 に答える