Laravel orderBy Not Passing to View -


i pulling collection database table using following code.

$profilevisits = db::table('recently_visited')     ->where('visitor_id',auth::user()->id)     ->orderby('times_visited', 'desc')->get();      return view('profile.index')         ->with([             'user' => $user,             'posts' => $posts,             'profilevisits' => $profilevisits,         ]); 

when dd($profilevisits) shows collection correct descending results. however, when pull info view using following code, doesn't descend it's supposed to. orderby query builder being undone or "return view..." code?

code in view...

@if (!$user->profilevisits->count())  @else     @foreach ($user->profilevisits $topvisits)         <p>{{ $topvisits->getusername() }}</p>     @endforeach @endif 

user model:

public function profilevisits()     {         return $this->belongstomany('app\models\user', 'recently_visited', 'visitor_id', 'profile_id');     }      public function addprofilevisits(user $user)     {         $this->profilevisits()->attach($user->id);     }      public function previouslyvisited(user $user)     {         return (bool) $this->profilevisits()->get()->where('id', $user->id)->count();     } 

actually, figured out. on user model, added orderby instead of using in controller. see below.

user model:

public function profilevisits() {     return $this->belongstomany('app\models\user', 'recently_visited', 'visitor_id', 'profile_id')->orderby('times_visited', 'desc'); } 

Comments