在树枝中访问雄辩的BelongsToMany?

时间:2016-11-08 15:43:56

标签: symfony eloquent twig

我在雄辩的关系中遇到问题,并将它们加载到树枝模板中。我有三个表,文章,用户和作者。第一个也很直接他们是我的博客文章(文章),用户是我的用户模型,包含名字,姓氏,电子邮件等。作者是一个数据透视表,其中包含(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)
                        "matt@website.com" [
                            "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)
                        "matt@website.com" [
                            "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 个答案:

答案 0 :(得分:1)

通过将作者定义为belongsToMany Article,它将为您提供一个数组。您的查询正在检索您想要的数据,但如果只有一位作者应该在您的模板中检索:

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

<强>更新

对于多位作者,如果您希望每位作者都有自己的“by”行,您可以这样做:

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