2016-11-08 7 views
0

私は雄弁に関係する問題を持っていて、それを小枝テンプレートで読み込んでいます。私は記事、ユーザー、著者の3つのテーブルを持っています。最初はあまりにもまっすぐで、彼らは私のブログ記事(記事)で、ユーザーはファーストネーム、ラストネーム、メールなどを含む私のユーザーモデルです。 Authorsは、前の2つのテーブルのidに対応する(users_idとarticles_id)を含むピボットテーブルです。雄弁にアクセスするBelongsToManyの小枝?

私が抱えている問題は、以下の配列をtwigマークアップで使用して著者の名前を表示する方法です。現時点では私はこれを持っていますが、ただ機能していません。

{% for article in articles %} 
by {{ article.first_name }} {{ article.last_name }} 
{% end for %} 

私はこれを数日間把握しようとしています。

MY CODE

私は、次の関係が私のルートの中で

class Article extends Eloquent { 

    public function authors() { 
     return $this->belongsToMany('voicetank\User\User', 'ArticleAuth', 'Articles_id', 'users_id'); 
    } 
} 

class User extends Eloquent { 
    public function articles() { 
     return $this->belongsToMany('voicetank\Articles\AddArticle', 'ArticleAuth', 'users_id', 'id'); 
} 

私は、次の配列を取得のvar_dumpを使用して、次のコード

$app->get('/home', function() use ($app) { 

$articles = $app->article->where('articlepublish', true)->orderBy('created_at', 'desc')->with('authors')->get(); 

$user = $app->user->get(); 

$app->render('home/home.php', [ 
    'articles' => $articles, 
    'user' => $user 
]); 

を持っています。

object(Illuminate\ Database\ Eloquent\ Collection) #199 (1) { ["items":protected]= > 
array(1) { 
    [0] => object(voicetank\ Articles\ AddArticle) #194 (23) { ["table":protected]= > 
     string(8) 
    "Articles" ["fillable": protected] => array(5) { 
     [0] => string(5) 
     "title" [1] => string(4) 
     "body" [2] => string(14) 
     "featurepicture" [3] => string(14) 
     "articlepublish" [4] => string(8) 
     "category" 
    }["connection": protected] => NULL["primaryKey": 
     protected] => string(2) 
    "id" ["perPage": protected] => int(15)["incrementing"] => 
     bool(true)["timestamps"] => bool(true)["attributes": 
      protected] => array(8) { 
      ["id"] => int(35)["title"] => string(17) 
      "This is the Title" ["body"] => string(15) 
      "sdfasdfasdfsadf" ["category"] => string(8) 
      "Category" ["featurepicture"] => string(11) 
      "picturelink" ["articlepublish"] => int(1)[ 
       "updated_at"] => string(19) 
      "2016-11-08 14:57:26" ["created_at"] => string(19) 
      "2016-11-08 14:57:26" 
     }["original": protected] => array(8) { 
      ["id"] => int(35)["title"] => string(17) 
      "This is the Title" ["body"] => string(15) 
      "sdfasdfasdfsadf" ["category"] => string(8) 
      "Category" ["featurepicture"] => string(11) 
      "picturelink" ["articlepublish"] => int(1)[ 
       "updated_at"] => string(19) 
      "2016-11-08 14:57:26" ["created_at"] => string(19) 
      "2016-11-08 14:57:26" 
     }["relations": protected] => array(1) { 
      ["authors"] => object(Illuminate\ Database\ Eloquent\ Collection) #202 (1) { ["items":protected]= > 
       array(1) { 
        [0] => object(voicetank\ User\ User) #201 (23) { ["table":protected]= > 
         string(5) 
        "users" ["fillable": protected] => array(10) { 
         [0] => string(5) 
         "email" [1] => string(8) 
         "password" [2] => string(10) 
         "first_name" [3] => string(9) 
         "last_name" [4] => string(15) 
         "current_project" [5] => string(6) 
         "active" [6] => string(11) 
         "active_hash" [7] => string(12) 
         "recover_hash" [8] => string(19) 
         "remember_identifier" [9] => string(
          14) 
         "remember_token" 
        }["connection": protected] => NULL[ 
         "primaryKey": protected] => string(2) 
        "id" ["perPage": protected] => int(15)[ 
         "incrementing"] => bool(true)[ 
         "timestamps"] => bool(true)[ 
         "attributes": protected] => array(13) { 
         ["id"] => int(29)["email"] => string(
          24) 
         "[email protected]" [ 
          "first_name" 
         ] => string(7) 
         "Matthew" ["last_name"] => string(6) 
         "Rogers" ["password"] => string(60) 
         "xxxxxxxx" [ 
          "current_project" 
         ] => int(1)["active"] => int(1)[ 
           "active_hash"] => NULL[ 
           "recover_hash"] => NULL[ 
           "remember_identifier"] => 
          NULL["remember_token"] => NULL[ 
           "created_at"] => string(19) 
         "2016-08-07 08:59:01" ["updated_at"] => 
         int(2016) 
        }["original": protected] => array(15) { 
         ["id"] => int(29)["email"] => string(
          24) 
         "[email protected]" [ 
          "first_name" 
         ] => string(7) 
         "Matthew" ["last_name"] => string(6) 
         "Rogers" ["password"] => string(60) 
         "xxxxxxxxxxxxxx" [ 
          "current_project" 
         ] => int(1)["active"] => int(1)[ 
           "active_hash"] => NULL[ 
           "recover_hash"] => NULL[ 
           "remember_identifier"] => 
          NULL["remember_token"] => NULL[ 
           "created_at"] => string(19) 
         "2016-08-07 08:59:01" ["updated_at"] => 
         int(2016)["pivot_Articles_id"] => int(
          35)["pivot_users_id"] => int(
          29) 
        }["relations": protected] => array(1) { 
         ["pivot"] => object(Illuminate\ Database\ Eloquent\ Relations\ Pivot) #200 (26) { ["parent":protected]= > 
          object(voicetank\ Articles\ AddArticle) #191 (23) { ["table":protected]= > 
          string(8) 
         "Articles" ["fillable": protected] => 
         array(5) { 
           [0] => string(5) 
           "title" [1] => string(4) 
           "body" [2] => string(14) 
           "featurepicture" [3] => 
           string(14) 
           "articlepublish" [4] => 
           string(8) 
           "category" 
          }["connection": protected] => 
          NULL["primaryKey": protected] => 
          string(2) 
         "id" ["perPage": protected] => int(15)[ 
           "incrementing"] => bool(true)[ 
           "timestamps"] => bool(true)[ 
           "attributes": protected] => 
          array(0) {}["original": protected] => 
          array(0) {}["relations": 
           protected 
          ] => array(0) {}["hidden": 
           protected 
          ] => array(0) {}["visible": 
           protected 
          ] => array(0) {}["appends": 
           protected 
          ] => array(0) {}["guarded": 
           protected 
          ] => array(1) { 
           [0] => string(1) 
           "*" 
          }["dates": protected] => array(0) {} 
          ["dateFormat": protected] => NULL[ 
           "casts": protected] => array(
           0) {}["touches": protected] => 
          array(0) {}["observables": 
           protected 
          ] => array(0) {}["with": 
           protected 
          ] => array(0) {}["morphClass": 
           protected 
          ] => NULL["exists"] => bool(false)[ 
           "wasRecentlyCreated"] => bool(
           false) 
        }["foreignKey": protected] => string(11) 
        "Articles_id" ["otherKey": protected] => 
        string(8) 
        "users_id" ["guarded": protected] => array(0) {} 
         ["connection": protected] => NULL["table": 
          protected] => string(11) 
        "ArticleAuth" ["primaryKey": protected] => 
        string(2) 
        "id" ["perPage": protected] => int(15)[ 
          "incrementing"] => bool(true)[ 
          "timestamps"] => bool(false)[ 
          "attributes": protected] => array(2) { 
          ["Articles_id"] => int(35)["users_id"] => 
           int(29) 
         }["original": protected] => array(2) { 
          ["Articles_id"] => int(35)["users_id"] => 
           int(29) 
         }["relations": protected] => array(0) {}[ 
          "hidden": protected 
         ] => array(0) {}["visible": protected] => 
         array(0) {}["appends": protected] => 
         array(0) {}["fillable": protected] => 
         array(0) {}["dates": protected] => array(
          0) {}["dateFormat": protected] => 
         NULL["casts": protected] => array(0) {}[ 
          "touches": protected 
         ] => array(0) {}["observables": protected] => 
         array(0) {}["with": protected] => array(0) {} 
         ["morphClass": protected] => NULL[ 
          "exists"] => bool(true)[ 
          "wasRecentlyCreated"] => bool(false) 
       } 
     }["hidden": protected] => array(0) {}["visible": 
      protected 
     ] => array(0) {}["appends": protected] => array(0) {}[ 
      "guarded": protected 
     ] => array(1) { 
      [0] => string(1) 
      "*" 
     }["dates": protected] => array(0) {}["dateFormat": 
      protected 
     ] => NULL["casts": protected] => array(0) {}["touches": 
      protected 
     ] => array(0) {}["observables": protected] => array(0) {} 
     ["with": protected] => array(0) {}["morphClass": 
      protected 
     ] => NULL["exists"] => bool(true)["wasRecentlyCreated"] => 
     bool(false) 
} 
} 
} 
}["hidden": protected] => array(0) {}["visible": protected] => array(
    0) {}["appends": protected] => array(0) {}["guarded": 
    protected 
] => array(1) { 
    [0] => string(1) 
    "*" 
}["dates": protected] => array(0) {}["dateFormat": protected] => 
NULL["casts": protected] => array(0) {}["touches": protected] => 
array(0) {}["observables": protected] => array(0) {}["with": 
    protected 
] => array(0) {}["morphClass": protected] => NULL["exists"] => 
bool(true)["wasRecentlyCreated"] => bool(false) 
} 
} 
} 

答えて

1

ArticlebelongsToManyとして著者を定義することで、あなたの配列を与えるために起こっています。あなたはそれぞれの作者が自分を持っているしたい場合は、複数の投稿者

{% for article in articles %} 
by {{ article.authors[0].first_name }} {{ article.authors[0].last_name }} 
{% end for %} 

UPDATE:あなたがやるべきただ一つの作者がある場合、クエリは、あなたがしたいが、あなたのテンプレート内のデータを検索していますライン「で」あなたが行うことができます:

{% for article in articles %} 
    {% for author in authors %} 
     by {{ author.first_name }} {{ author.last_name }} 
    {% end for %} 
{% end for %} 

をそれとも、すべてにそれらをしたい場合は、カンマで区切られたラインで同じになり、それは次のようになります。

{% for article in articles %} 
    by 
    {% for author in authors %} 
     {{ author.first_name }} {{ author.last_name }}{% if not loop.last %}, {% endif %} 
    {% end for %} 
{% end for %} 
+0

これは、感謝のエリックです。 –

+0

こんにちはエリック、ちょっと質問があります。複数の著者のためにこれをどのように構造化するのですか? –

+0

レスポンスが更新されました –