简体   繁体   English

Laravel追加到集合模型中的每个模型

[英]Laravel append to every model in collection model

I am developing an api. 我正在开发一个api。 I have a function that returns all models in database. 我有一个返回数据库中所有模型的函数。 I wan't to append to every model of that collection the related model. 我不想将相关模型附加到该集合的每个模型中。

public function index()
    {
        if(auth()->user()->hasPermission('read-company_requests')) {
            $createCompanyRequestments = CreateCompanyRequest::orderBy('status')->get();
        } elseif(auth()->user()->hasPermission('read-company_request')) {
            $createCompanyRequestments = auth()->user()->companyCreateRequestment()->orderBy('status')->get();
        } else {abort(403);}

        return $createCompanyRequestments->toJson();
    }

So basicly every CreateCompanyRequest::orderBy('status')->get(); 因此,基本上每个CreateCompanyRequest::orderBy('status')->get(); should have the related model company in the response. 应对中应有相关的样板company How can I do this? 我怎样才能做到这一点?

像这样使用渴望加载

CreateCompanyRequest::orderBy('status')->with('company')->get()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM