简体   繁体   English

Laravel:调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: where()

[英]Laravel: Call to undefined method Illuminate\Database\Eloquent\Collection::where()

I am having a problem in this controller function. 我在此控制器功能中遇到问题。 The function takes an optional 'search' parameter and uses it to search through en employers available jobs for keywords. 该函数采用可选的“搜索”参数,并使用它在整个雇主可用职位中搜索关键字。 When I am calling this function I am getting the following error. 当我调用此函数时,出现以下错误。

Call to undefined method Illuminate\\Database\\Eloquent\\Collection::where() 调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: where()

在此处输入图片说明

Here is my relevant code. 这是我的相关代码。 Any advice would be much appreciated! 任何建议将不胜感激!

Routes: (prefix: '/api/v1/') 路由:(前缀:“ / api / v1 /”)

Route::get('employer/{employerId}/jobs', 'EmployersController@getJobs');

Controller: 控制器:

public function getJobs ($employerId) {

    $search = Input::get('query');

    $jobs = Job::getAvailableByEmployer($employerId, $search);
    return $jobs;
}

Model: 模型:

public static function getAvailableByEmployer($employerId, $search=NULL)
{
    $jobs = Job::where('jobs.employer_id', '=', $employerId)
                ->where('jobs.status', '=', 'Available')
                ->orderBy('jobs.created_at', 'desc')
                ->get();

    if ($search)
    {
        $jobs->where('title', 'LIKE', '%'. $search .'%')
            ->orWhere('description', 'LIKE', '%'. $search .'%');
  }

        return $jobs;
}

Make your function like this: 使您的功能是这样的:

public function scopeGetAvailableByEmployer($query, $employerId, $search=NULL)
{
    $query->where('employer_id', '=', $employerId)
            ->where('status', '=', 'Available')
            ->orderBy('created_at', 'desc');


    if ($search)
    {
        $jobs->where('title', 'LIKE', '%'. $search .'%')
             ->orWhere('description', 'LIKE', '%'. $search .'%');
    }

    return $query;
}

Now call it like ( scope lets you call statically): 现在称呼它( scope让您可以静态调用):

$jobs = Job::getAvailableByEmployer($employerId, $search)->get();

Also remember that, once you call get() then you can't make any query because that is not a Query Builder anymore but a Collection object. 还要记住,一旦调用get() ,就无法进行任何查询,因为它不再是Query Builder ,而是Collection对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel-eloquent:调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: where() - Laravel-eloquent: Call to undefined method Illuminate\Database\Eloquent\Collection::where() 调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: save()laravel - Call to undefined method Illuminate\Database\Eloquent\Collection::save() laravel Laravel 5 - 调用未定义的方法 Illuminate\Database\Eloquent\Collection::Paginate() - Laravel 5 - Call to undefined method Illuminate\Database\Eloquent\Collection::Paginate() Laravel 5:调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: exists() - Laravel 5: Call to undefined method Illuminate\Database\Eloquent\Collection::exists() Laravel 5调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: attach() - Laravel 5 Call to undefined method Illuminate\Database\Eloquent\Collection::attach() Laravel 5:无法使用分页,出现错误“调用未定义的方法 Illuminate\\Database\\Eloquent\\Collection::render()” - Laravel 5: not able to use pagination, I get error “Call to undefined method Illuminate\Database\Eloquent\Collection::render()” 调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: useAsCallable()“ - Call to undefined method Illuminate\Database\Eloquent\Collection::useAsCallable()" 调用未定义的方法 Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::type() [Laravel] - Call to undefined method Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::type() [Laravel] Laravel 分页 - 调用未定义的方法 Illuminate\\Database\\Eloquent\\Builder::links() - Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links() Laravel 7.6 调用未定义的方法 Illuminate\Database\Eloquent\Builder::appends() - Laravel 7.6 Call to undefined method Illuminate\Database\Eloquent\Builder::appends()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM