user0129e021939232
user0129e021939232

Reputation: 6355

laravel blade show count of rows in template

Hi I am retuning the count of some rows using eloquent as so in my repository:

public function countOpenProjects(){
        $value = 'Open';

        //return \Auth::user()->projects()->count();
        return  \Project::with(['status'])
            ->whereHas('status', function($q) use($value) {
                // Query the name field in status table
                $q->where('name', '=', $value); // '=' is optional
            })
            ->whereUserId(Auth::user()->id)
            ->count();
    }

and in my users controller I call this as so:

 public function counttest()
    {

        $usersprojects = $this->userrepo->countOpenProjects();

        return $usersprojects;


    }

The count is returned correctly. But i'm not sure how to output this into a blade format, does anyone know how?

Upvotes: 1

Views: 2432

Answers (2)

Alfonso Sánchez
Alfonso Sánchez

Reputation: 41

You have 3 methods in order to pass arguments to your views from the controller:

Several ->with()

return View::make('projects')->with('usersprojects', $usersprojects);

Several ->withVariable()

return View::make('projects')->withUsersprojects($usersprojects);

Simple , compact('variablename')

return View::make('projects', compact('usersprojects'));

Then you can access them in a Blade view using {{ $variablename }}

Upvotes: 0

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Can't you just pass it to your view?

class ProjectsController extends Controller {

    public function counttest()
    {
        $usersprojects = $this->userrepo->countOpenProjects();

        return View::make('projects')->with('usersprojects', $usersprojects);
    }

} 

And in your view:

Project Count: {{ $usersprojects }}

Upvotes: 2

Related Questions