Rocky

Diplay Activity where user has replied to

Posted 2 years ago by Rocky

I've got 2 tables:

Activity id | userid | text | userid_deleted | date_deleted

ActivityComment id | activityid | userid | date_created | text | userid_deleted | date_deleted

I'm fetching the Activity and the ActivityComment like:

$activities = Activity::with('Trainer.Avatar', 'Comments.Trainer.Avatar')
        ->where('userid', '=', $trainerid)
        ->where('date_deleted', '=', '0')
        ->orderBy('date_last_activity', 'desc')
        ->paginate(10);

But now i need to display also the activity where a "Trainer" has just posted a comment (the whole Activity with Comments where user replied to)

How would i do that?

ActivityModel

<?php

namespace App\Models\Feed;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
    protected $table = 'activity';
    public $timestamps = false;

    public function Trainer() {
        return $this->belongsTo('App\Models\Trainer\Trainer', 'userid');
    }

    public function Comments() {
        return $this->hasMany('App\Models\Feed\ActivityComment', 'activityid')
        ->where('date_deleted', '=', '0')
        ->orderBy('date_created', 'asc');
    }
}

ActivityCommentModel

<?php

namespace App\Models\Feed;

use Illuminate\Database\Eloquent\Model;

class ActivityComment extends Model
{
    protected $table = 'activity_comment';
    public $timestamps = false;

    public function Trainer() {
        return $this->belongsTo('App\Models\Trainer\Trainer', 'userid');
    }
}

Please sign in or create an account to participate in this conversation.