Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brentxscholl's avatar

How do I filter my DB query results to not display any duplicate foreign keys? Laravel

I'm a novice at php and creating a project using Laravel. I'm not sure the correct way to even ask this question so I will begin explaining.

User profiles have a section that displays a list of activities a user participated in. User can post the activities they participated in multiple times. I want to list all activities a user posted on their profile. I do not want any activity to be listed more than once. So if a user plays hockey MORE THAN ONCE I only want to show hockey in the list once.

I have 3 tables for this relationship

users table:

| id | user_name | updated_at | created_at |
--------------------------------------------
| 1  | JohnSmith |            | xxxxxxxxxx |

activities table:

| id | title   | description | created_at | updated_at |
--------------------------------------------------------
| 1  | Hockey  | xxxxxxxxxxx | xxxxxxxxxx |            |
| 2  | Soccer  | xxxxxxxxxxx | xxxxxxxxxx |            |
| 3  | Skiing  | xxxxxxxxxxx | xxxxxxxxxx |            |
| 4  | Biking  | xxxxxxxxxxx | xxxxxxxxxx |            |
| 5  | Running | xxxxxxxxxxx | xxxxxxxxxx |            |
| 6  | Camping | xxxxxxxxxxx | xxxxxxxxxx |            |

resource table (each row represents a post):

| id | user_id | activity_id | created_at | updated_at |
-------------------------------------------------------
| 1  | 1       | 1          | xxxxxxxxxx |            |
| 2  | 1       | 2          | xxxxxxxxxx |            |
| 3  | 1       | 1          | xxxxxxxxxx |            |
| 4  | 1       | 3          | xxxxxxxxxx |            |
| 5  | 1       | 3          | xxxxxxxxxx |            |
| 6  | 1       | 1          | xxxxxxxxxx |            |
| 7  | 1       | 5          | xxxxxxxxxx |            |

MODELS

User model:

class User extends Eloquent {

    public function posts(){
        return $this->hasMany('Acme\Resource\Resource');
    }

}

Activity Model:

class Activity extends Eloquent  {

    public function resource(){
        return $this->hasMany('Acme\Resource\Resource');
    }
}

Resource Model:

class Resource extends Eloquent {

    public function user()
    {
        return $this->belongsTo('Acme\Users\User', 'user_id');
    }
    public function activity()
    {
        return $this->belongsTo('Acme\Activities\Activity', 'activity_id');
    }
}

Controller method that displays page showing list of activities:

public function show($id)
    {
        $posts = $this->postRepository->getAllActivities($id);
        $user = $this->userRepository->findById($id);

        return View::make('profile', compact('user', 'posts'));
    }

PostRepository:

public function getAllActivities($id)
    {
        return Resource::with('activity')->where('user_id', '=', $id)->get();
    }

UserRepository:

public function findById($id)
    {
        return User::findOrFail($id);
    }

In my view I'm able to retrieve all activities for a user like this:

<ul>
    @foreach ( $posts as $post )
        <li>{{ $post->activity->title }}</li>
    @endforeach
</ul>

My result is:

  • Hockey
  • Soccer
  • Hockey
  • Skiing
  • Skiing
  • Hockey
  • Running

However I don't want any activities to repeat. I only want to know if a user did the activity, not every instance of the activity. Is there a way to get only one instance when it appears multiple times?

My ideal output would be

  • Hockey
  • Soccer
  • Skiing
  • Running
0 likes
2 replies
JarekTkaczyk's avatar
Level 53

Just change this method:

public function getActivitiesForUser($userId)
{
    // I assume resources is the relation name, instead of singular resource
    return Activity::whereHas('resources', function ($q) use ($userId) {
      $q->where('user_id', $userId);
    })->get();
}

It doesn't make sense to have a method getAllActivities that returns Resources instead of Activities, and not all of them, but only for given user.

Please or to participate in this conversation.