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

anonymouse703's avatar

How to attach query data from model and display it in view per user?

Hello guys, how to attach query data from model to display in view per user?

I have a query here to get the average rating in my user model.

public static  function getRate($rate){
        
        $rateUser = DB::table('users')
                    ->join('rating', 'users.id', '=', 'rating.user_id')
                    ->select('users.*', DB::raw( 'AVG( rating.rating ) as ratings_average'))
                    ->groupBy('id')
                    ->orderBy('ratings_average', 'rating')
                    ->get();
        return $rateUser;            
        // dd($rateUser);
    }

This is the result +"ratings_average": 4.0

And I have something like this in my controller where the $rateUser will show to all users not individually... how to attached this per user in controller?

public function __construct(User $user)
    {
            $this->user = $user;
    }
        
    public function index(){
            // $users = User::all();
            $rateUser ='';
            $users = User::with('skills')
                            ->with('achievements')
                            ->with('rateDev')
                            ->get();
                            
            $skillsList = Skill::all();

            $rateUser = $this->user->getRate($rateUser);


        
        // dd($rateUser);
    
        return view('pages.dashboard.index')
            ->with('users', $users)
            ->with('rateUser',$rateUser)
            ->with('skillsList', $skillsList);
    }

Thank you and God bless.

0 likes
0 replies

Please or to participate in this conversation.