2016-11-08 44 views
0

我有一个关于口才的问题,并将它们加载到树枝模板中。我有三个表格,分别是文章,用户和作者。第一个也很直接,他们是我的博客帖子(文章),用户是我的用户模型,包含名字,姓氏,电子邮件等。作者是一个数据透视表,其中包含(users_id和articles_id),它与前两个表中的id相对应。访问雄辩属于许多在树枝?

我遇到的问题是我如何使用下面的数组显示作者的名字和枝条标记。此刻我有这个,但它只是不工作。

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

任何帮助将是伟大的,我一直试图找出这几天。

我的代码

我有以下关系

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

在我的路线,我有以下代码

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

使用的var_dump我碰到下面的阵列。

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

通过定义作者为Article一个belongsToMany它会给你一个数组。您的查询检索你想,但在你的模板中的数据,如果只有一个作者,你应该做的:如果你想每个作者都有自己的

对于多位作者:

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

更新了回复,让我知道这是否适合你 –