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

Posted 2 months ago by anonymouse703

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.

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

Reply to

Use Markdown with GitHub-flavored code blocks.