简体   繁体   English

在雄辩的资源中使用分页会在Laravel中引发Undefined Property $ id错误

[英]Using pagination in Eloquent resource throws Undefined Property $id error in Laravel

I'm trying to build a application in Laravel 5.7 where I am having a model named CompanyBehaviour : 我试图建立一个应用程序Laravel 5.7 ,其中我有一个名为模型CompanyBehaviour

protected $table = 'company_behaviour';

protected $guarded= [];

public function companies()
{
    return $this->belongsTo('Noetic\Plugins\Conxn\Models\Company','company_id','id');
}

public function companyRole()
{
    return $this->belongsTo('Noetic\Plugins\Conxn\Models\Variables\Company\Role', 'company_role_id', 'id');
}

public function companySpecialisation()
{
    return $this->belongsTo('Noetic\Plugins\Conxn\Models\Variables\Company\Role', 'company_specialisation_id', 'id');
}

I'm having a resource file for this: 我正在为此提供资源文件:

class CompanyBehaviourResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'company' => $this->company->name,
            'company_role' => $this->companyRole->name,
            'company_specialisation' => $this->companySpecialisation->name,
        ];
    }
}

Which I need to use while fetching the data in my controller: 在获取控制器中的数据时需要使用的内容:

public function index(Request $request)
{

    return new CompanyBehaviourResource(CompanyBehaviour::whereHas('companies', function ($q) use($request) {
        $q->where('name', 'like', '%'.$request->search.'%');
    })->orderBy('created_at', 'desc')
        ->paginate(20)
    );
}

But when I called this I'm getting pagination error: 但是当我打电话给我时,我得到了分页错误:

Undefined property: Illuminate\\Pagination\\LengthAwarePaginator::$id 未定义的属性:Illuminate \\ Pagination \\ LengthAwarePaginator :: $ id

And the trace represents that: 跟踪表示:

class: "Illuminate\Http\Resources\Json\JsonResource"
file: "D:\~Resource\CompanyBehaviourResource.php"
function: "__get"
line: 17
type: "->"

Stating the 'id' => $this->id, this line. 在这一行中说明'id' => $this->id, I've gone through the documentation of Laravel Resource API I'm couldn't find where I'm doing wrong. 我浏览了Laravel Resource API的文档, 找不到我做错的地方。 Help me out with this. 帮我解决这个问题。 Thanks. 谢谢。

Your call returns eloquent collection, instead of single result, thus not holding $id property. 您的呼叫返回的是雄辩的集合,而不是单一的结果,因此不持有$id属性。 You should call collection method instead: 您应该改为调用collection方法:

public function index(Request $request)
{

    return new CompanyBehaviourResource::collection(CompanyBehaviour::whereHas('companies', function ($q) use($request) {
        $q->where('name', 'like', '%'.$request->search.'%');
    })->orderBy('created_at', 'desc')
        ->paginate(20)
    );
}

暂无
暂无

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

相关问题 Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id 使用Eloquent在Laravel中的未定义属性 - Undefined property in Laravel using Eloquent Laravel Eloquent HasOne :: $ id未定义属性 - Laravel Eloquent HasOne::$id Undefined Property Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error 使用Laravel雄辩的资源来犯错误 - Using the Laravel Eloquent Resource to errors “属性 [id] 在 Eloquent 构建器实例上不存在。”,laravel 8 Api 资源 - "Property [id] does not exist on the Eloquent builder instance.", laravel 8 Api Resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM