3

I am trying to pass two parameters to a function in whereHas with Laravel because otherwhys it wont be able to use the $businessid but it's throwing an error, can anyone help?

Error:

FatalThrowableError in HomeController.php line 16: Type error: Too few arguments to function App\Http\Controllers\Business\User\HomeController::App\Http\Controllers\Business\User{closure}(), 1 passed in C:\web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php on line 938 and exactly 2 expected


Code:

<?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 Answer 1

9

This is how you can pass variable(s) to the function..

        $workerCount = Player::whereHas("roleplay", function($q2) use ($businessid) {
            $q2->where('business_id', $businessid);
        })->count();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.