theGreenCabbage
theGreenCabbage

Reputation: 4845

Laravel scope query must be of the type array, string given

I am using scope queries within Laravel's Models to assign custom query functions. This particular function has been giving me issues telling me I must supply the parameter as an array, and not a String.

/**
 * Get all labs of a class
 * TODO: Fix error
 * @param $query
 * @param $subjectCode Course subject code i.e. "PHYS"
 * @param $courseNo    Course # i.e. "101" or "%" for everything
 * @return mixed       A list of lectures of the class
 */
public function scopeLabsByClass($query, $subjectCode, $courseNo) {
    return $query
        ->where('subject_code', 'like', $subjectCode)
        ->where('course_no', 'like', $courseNo)
        ->whereIn('instr_type', 'like', LAB)
        ;
}

When I run it using Tinker:

>>> App\SchoolClass::LabsByClass('ece', '201')->get();

I get the following error.

PHP error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /home/vagrant/Code/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 311 and defined in /home/vagrant/Code/laravel/vendor/laravel/framework/src/Illuminate/Database/Grammar.php on line 113

The code I have is similar to the tutorial from Laracasts where the first input to the scope method is the $query

Upvotes: 1

Views: 4456

Answers (2)

theGreenCabbage
theGreenCabbage

Reputation: 4845

I was playing around with the whereIn function but forgot to remove it when I went back to my original solution.

/**
 * Get all labs of a class
 * TODO: Fix error
 * @param $query
 * @param $subjectCode Course subject code i.e. "PHYS"
 * @param $courseNo    Course # i.e. "101" or "%" for everything
 * @return mixed       A list of lectures of the class
 */
public function scopeLabsByClass($query, $subjectCode, $courseNo) {
    return $query
        ->where('subject_code', 'like', $subjectCode)
        ->where('course_no', 'like', $courseNo)
        ->where('instr_type', 'like', LAB)
        // LAB is a String constant defined on the top of the doc
        ;
}

Upvotes: 1

whoacowboy
whoacowboy

Reputation: 7447

Try setting the L in Labs to lowercase.

App\SchoolClass::labsByClass('ece', '201')->get();

Upvotes: 1

Related Questions