Laravel:将参数传递给函数?

时间:2017-04-23 20:16:33

标签: php laravel

我正在尝试将两个参数传递给whereHas with Laravel中的函数,因为其他情况下它不能使用$businessid但是它会抛出错误,有人可以帮忙吗?

错误:

  

HomeController.php第16行中的FatalThrowableError:输入错误:太少   功能的参数   应用程序\ HTTP \控制器\企业\用户\的HomeController ::应用程序\ HTTP \控制器\企业\用户{}关闭()   1传入   C:\网络\供应商\ laravel \框架的\ src \照亮\数据库\雄辩\ Builder.php   在第938行和预期的2个


代码:

<?php

namespace App\Http\Controllers\Business\User;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Database\Frontend\Roleplay\Business\Businesses;
use App\Database\Frontend\User\Player;

class HomeController extends Controller
{
    public function getView(Request $request, $businessid)
    {
        $workerCount = Player::whereHas("roleplay", function($q2, $businessid) {
            $q2->where('business_id', $businessid);
        })->count();

        $workersWorkingCount = Player::where('currently_working', '1')->whereHas("roleplay", function($q2, $businessid) {
            $q2->where('business_id', $businessid);
        })->count();

        $workersOnlineCount = Player::where('online', '1')->whereHas("roleplay", function($q2, $businessid) {
            $q2->where('business_id', $businessid);
        })->count();

        $workersOfflineCount = Player::where('online', '0')->whereHas("roleplay", function($q2, $businessid) {
            $q2->where('business_id', $businessid);
        })->count();

        return view('business.home', compact(
            'workerCount',
            'workersWorkingCount',
            'workersOnlineCount',
            'workersOfflineCount'));
    }
}

1 个答案:

答案 0 :(得分:7)

这是你可以将变量传递给函数的方法..

        $workerCount = Player::whereHas("roleplay", function($q2) use ($businessid) {
            $q2->where('business_id', $businessid);
        })->count();