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

Posted 5 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', '', '=', 'rating.user_id')
                    ->select('users.*', DB::raw( 'AVG( rating.rating ) as ratings_average'))
                    ->orderBy('ratings_average', 'rating')
        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')
            $skillsList = Skill::all();

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

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

Thank you and God bless.

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