【问题标题】:How to alias in Laravel Eager Loading Array without changing method name如何在 Laravel Eager Loading Array 中使用别名而不更改方法名称
【发布时间】:2021-11-22 02:10:43
【问题描述】:

我在执行 Laravel 预加载时需要别名:

$employee = Employee::with(['profilepic', 'emp_company'])->find($user->employee_id);

在我的模型中,我有 profilepic 函数和 emp_company 函数。我需要传递一个公司对象而不将 emp_company 函数重命名为 company。

我需要这样做,因为我希望 JSON 响应作为 profilepic 和 company。对于前端,我需要 emp_company 对象作为公司。可以别名。

【问题讨论】:

    标签: php laravel-5 eloquent


    【解决方案1】:

    在数组中将您的 JSON 命名为 "company"

    试试这个:

    $employee = Employee::with(['profilepic' => 'profilepic', 'company' => 'emp_company'])->find($user->employee_id);
    

    【讨论】:

    • 这个不行,得到 500 服务器错误
    • 还给“profilepic”一个上面更新的索引。
    猜你喜欢
    • 2014-10-22
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2021-10-17
    • 2018-01-25
    • 1970-01-01
    相关资源
    最近更新 更多