【问题标题】:laravel blade show count of rows in templatelaravel 刀片显示模板中的行数
【发布时间】:2014-11-07 06:37:21
【问题描述】:

您好,我正在我的存储库中使用 eloquent 重新调整某些行的计数:

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();
    }

在我的用户控制器中,我这样称呼它:

 public function counttest()
    {

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

        return $usersprojects;


    }

计数正确返回。但是我不确定如何将其输出为刀片格式,有人知道吗?

【问题讨论】:

    标签: laravel count eloquent blade


    【解决方案1】:

    你不能把它传递给你的视图吗?

    class ProjectsController extends Controller {
    
        public function counttest()
        {
            $usersprojects = $this->userrepo->countOpenProjects();
    
            return View::make('projects')->with('usersprojects', $usersprojects);
        }
    
    } 
    

    在你看来:

    Project Count: {{ $usersprojects }}
    

    【讨论】:

      【解决方案2】:

      您有 3 种方法可以将参数从控制器传递给您的视图:

      几个 ->with()

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

      几个 ->withVariable()

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

      简单,紧凑('变量名')

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

      然后您可以使用 {{ $variablename }}

      在 Blade 视图中访问它们

      【讨论】:

        猜你喜欢
        • 2014-09-14
        • 1970-01-01
        • 2019-01-15
        • 2014-11-18
        • 2018-09-24
        • 2018-09-05
        • 2018-03-14
        • 2020-09-22
        • 2013-04-29
        相关资源
        最近更新 更多