Twig嵌套的foreach循环不显示结果

时间:2017-06-21 11:10:15

标签: twig slim-3

我正在为我的数据库中的用户构建一个简单的聊天系统,我希望用户能够回复消息,但要按时间顺序显示。我无法获得嵌套的foreach循环来显示任何结果。这是我到目前为止所拥有的。

朋友数据库包含我users表中的所有用户,ID为215

| id | user_id | friend_id | accepted |
|  1 |      15 |         2 |        1 |

状态数据库

| id | user_id | parent_id | body             |
| 28 |      15 |      NULL | Hello            |
| 29 |       2 |        28 | Hi, how are you  |

这是我的Members Controllerquery建设者。

public function members(Request $request, Response $response, $args) {
    $token = User::where('id', $this->auth->user()->id)->first();
    $order = Order::where('user_id', $this->auth->user()->id)->first();

    if(!$order) {
        $token = $args['token'];
        return $response->withRedirect($this->router->pathFor('membersPaymentPlan', compact('token')));
    }

    $statuses = Status::notReply()->where(function($query) {
        return $query->where('user_id', $this->auth->user()->id)->orWhereIn('user_id', $this->auth->user()->friends()->pluck('id'));
    })->orderBy('id', 'DESC')->get();

    return $this->view->render($response, 'members/index.php', compact('token', 'statuses'));
}

在我的User模型中,我有方法关系。

public function statuses() {
    return $this->hasMany('Base\Models\Messaging\Status', 'user_id');
}

public function friendsOfMine() {
    return $this->belongsToMany('Base\Models\User\User', 'friends', 'user_id', 'friend_id');
}

public function friendOf() {
    return $this->belongsToMany('Base\Models\User\User', 'friends', 'friend_id', 'user_id');
}

public function friends() {
    return $this->friendsOfMine()->wherePivot('accepted', true)->get()->merge($this->friendOf()->wherePivot('accepted', true)->get());
}

在我的Status模型中。我有方法关系。

public function user() {
    return $this->belongsTo('Base\Models\User\User', 'user_id');
}

public function scopeNotReply($query) {
    return $query->whereNull('parent_id');
}

public function replies() {
    return $this->hasMany('Base\Models\Messaging\Status', 'parent_id');
}

这就是问题所在。这是我foreach loop模板中的嵌套Twig

{% for status in statuses %} // This loop works ok showing all results
    {% for reply in status.replies() %} // HERE IS THE PROBLEM - No results
        <div class="from-them margin-bottom-10">
            <p class="nomargin">{{ reply.body | nl2br }}</p>
        </div>

        <div class="clearfix"></div>
    {% endfor %}
    <div class="from-me margin-bottom-10">
        <p class="nomargin">{{ status.body | nl2br }}</p>
    </div>

    <div class="clearfix"></div>
{% endfor %}

为什么嵌套的foreach loop不会显示status.replies()关系中的任何结果?我有什么不对吗?

0 个答案:

没有答案