Home > Enterprise >  My website displaying duplicate rows of records
My website displaying duplicate rows of records

Time:06-17

I'm trying to make the 'My Mentors' and 'My Mentees' page to display records related to the logged in user and were able to do that.

MentorController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class MentorController extends Controller
{
  public function index(Request $request)
  {
    $user = $request->user();
    
    // $goals = DB::table('goal_mentors')
    //     ->join('users', 'goal_mentors.mentor_id', '=', 'users.id')
    //     ->join('goals', 'goal_mentors.goal_id', '=', 'goals.id')
    //     ->select('goals.user_id', 'goal_mentors.mentor_id', 'users.username', 'goal_mentors.goal_id', 'goals.id', 'goals.title', 'goals.description', 'goals.created_at')
    //     ->distinct()
    //     ->get();

    $goals = DB::table('goal_mentors')
        ->join('users', 'goal_mentors.mentor_id', '=', 'users.id')
        ->join('goals', 'goal_mentors.goal_id', '=', 'goals.id')
        ->select('goals.user_id', 'goal_mentors.mentor_id', 'users.username', 'goal_mentors.goal_id', 'goals.id', 'goals.title', 'goals.description', 'goals.created_at')
        ->where('goals.user_id', "=", auth()->id())
        ->distinct()
        ->get();

    return view('my-mentors', ['user' => $user, 'goals' => $goals]);
   }
}

my-mentors.blade.php

@extends('layouts.app')

@section('title', 'My Mentors')

@php
$highlighted_page = 'my_mentors';
@endphp

@section('content')
<main id="main" >

<div >
    <button type="button"  onclick="history.back()">
        <i ></i>
    </button>
    <h1>My Mentors</h1>
    <nav>
        <ol >
            <li ><a href="{{ route('home') }}">Dashboard</a></li>
            <li >My Mentors</li>
        </ol>
    </nav>
</div><!-- End Page Title -->

<section >
    <div >

        <!-- Left side columns -->
        <div >
            <div >

                @foreach($goals as $goal1)
                @if(auth()->id() == $goal1->user_id)
                <!-- Goal Card -->
                <div >
                    <div >
                        <div >
                            <h5 >
                                <img src="{{ asset('/img/yor.jpg') }}" width="50" height="50" alt="Profile" >
                                {{ $goal1->username }}
                            </h5>
                            <div >
                                @foreach($goals as $goal2)
                                @if($goal1->mentor_id == $goal2->mentor_id)
                                <a href="goal-board-mentor.html" >
                                    <div >
                                        <h5 >{{ $goal2->title }}</h5>
                                        <small >{{ $goal2->created_at }}</small>
                                    </div>
                                    <p >{{ $goal2->description }}</p>
                                </a>
                                @endif
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div><!-- End Goal Card -->
                @endif
                @endforeach

            </div>
        </div>

    </div>
</section>
</main>
@endsection

Query Output

How the website look like now

Problem now is it iterating through all the output from the query using the loop and will print the same user with literally the same goals.

CodePudding user response:

not quite sure why do you start from middle table if you need to get goals with mentors for current user

select 
  *
from goals g
  join goal_mentors gm on g.id = gm.goal_id
  join users u on gm.mentor_id = u.id
where g.user_id = 51;

anyway, your case is classical many-to-many relation between users(mentors) and goals

//User model

  // these are user's goals
  public function goals(){
    return $this->hasMany(Goal::class);
  }

  // these are goals where user is mentor
  public function goalsAsMentor(){
    return $this->belongsToMany(Goal::class, 'goal_mentors', 'mentor_id', 
  'goal_id');
  }

// Goal model

  // this is reverse for user's goals
  public function user(){
    return $this->belongsTo(User::class);
  }

  // these are mentors for goal
  public function mentors(){
    return $this->belongsToMany(User::class, 'goal_mentors', 'goal_id', 'mentor_id');
  }

now for user you can take goals with attached mentors

// controller

/** @var User */
$user = auth()->user();

$goals = $user->goals()->with(['mentors'])->get();
return view('my-mentors', ['user' => $user, 'goals' => $goals]);

/*
result structure
array(
  [
    'id': 1,
    'title': '',
    'description': '',
    //other Goal fields
    mentors: [
      [
        'id': 1,
        'username': '',
        //other mentor(user) fields      
      ],
      // other mentors
    ]
  ],
  // other goals with same structure
)
*/

CodePudding user response:

Please check whether the following condition works.

@foreach($goals as $goal2)
 @if($goal1->mentor_id == $goal2->id)

  • Related