Mohcin Bounouara
Mohcin Bounouara

Reputation: 623

Show the users with most posts in laravel 5

I want to show Users with most post and thats what i'm do.. I try this in my AppServiceProvider.php:

view()->composer('questions.side2', function($view){
        $m = User::leftJoin('questions','users.id','=','questions.user_id')
            ->selectRaw('‌​us‌​er‌s​.*, count(questions.user_id) AS total')
            ->groupBy('users.id')
            ->orderBy('total','DESC')
            ->skip(0)
            ->take(5)
            ->get();

        $view->with('m', $users); 
    });

In My Side view :

@foreach($m as $user)

   {{ $user->name }}

@endforeach

but i still have this error: Undefined variable: m (View: C:\wamp\www\qq\resources\views\questions\side.blade.php) (View: C:\wamp\www\qq\resources\views\questions\side.blade.php)

Upvotes: 0

Views: 349

Answers (1)

Hussam Abd
Hussam Abd

Reputation: 21

You are sharing the variable in a wrong way because "with" function expects an array of data, moreover, you are sharing a wrong variable name.

The variables must be passed like this:

$view->with([
   'm' => $users
]);

Or using compact() method, and you must change $users to $m

$view->with(compact('m')); //Or => $view->with(['m' => $m])

Upvotes: 1

Related Questions