0

I am building a simple chat system for users within my DB, and I want the user to be able to reply to messages, but to show in chronological order. I cannot get the nested foreach loop to show any results. Here is what i have so far.

Friends Database with both users from my users table, id's 2 and 15.

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

Status Database

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

This is my Members Controller with the query builder.

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

In my User Model, I have method relationships.

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

In my Status Model. I have method relationships.

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

Here is where the problem is. This is my nested foreach loop within my Twig template.

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

Why won't the nested foreach loop show any results from the status.replies() relationship? What do I have wrong here?

John
  • 111
  • 10
  • As with anything php/twig related var_dump or for twig dump is your friend. Basically use that in the various places to see what is coming out of your sql or returned from functions – Richard Housham Jun 21 '17 at 13:44
  • 1
    I'm not sure but status.replies() call the relationship option but status.replies is populate by eloquent with the result ;) – ekans Jun 30 '17 at 10:12

0 Answers0