Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
Laravel: passing parameters to function?

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'));
    }
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!