Laravel eloquent获得额外的列值作为响应

时间:2017-02-17 22:41:46

标签: php sql laravel

我有一个名为user

的表格
id | country | people | women
1  | USA     |   8    | 3
2  | BD      |   9    | 5

在laravel 5.4中,我可以使用

获取所有表格数据
$user = User::all();

这将返回包含这些列的用户表。但我希望获得额外的列值man = people-women作为输出。 我怎么能得到这个?

3 个答案:

答案 0 :(得分:0)

您只需在模型中创建一个getter

的新属性
public function getMenAttribute()
{
    return $this->people - $this->women;
}

然后循环遍历$users

$users->each(function($user){
    // use $user->men;
});

答案 1 :(得分:0)

在用户模型中添加以下行:

protected $appends = ['man'];

    public function getManAttribute()
    {
        return $this->people  - $this->women;
    }

我没有测试过这个解决方案,但理论上它应该可行

页面底部的来源:https://laravel.com/docs/5.0/eloquent

如果有效,请告诉我。谢谢

答案 2 :(得分:0)

你可以创建一个新属性,这里是一个例子

\App\Models\Sede::with(
                [
                    'area_trabajos' => function($query) {
                        $query->select('*',DB::raw('"false" as borrado'));
                    },
                    'empresa',
                    'puestos'
                ])->addSelect('*',DB::raw('id*id as newColum'))->get()->toJson()

返回

[{"id":4,"created_at":"2018-06-01 04:56:57","updated_at":"2018-06-01 05:46:50","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Empresa_id":61,"numero_trabajadores":8,"nombre_sede":"ii","newColum":16,"area_trabajos":[{"id":1,"created_at":"2018-06-01 07:55:19","updated_at":"2018-06-01 07:55:19","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":2,"created_at":"2018-06-01 07:55:19","updated_at":"2018-06-01 07:55:19","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":3,"created_at":"2018-06-01 07:55:19","updated_at":"2018-06-01 07:55:19","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":4,"created_at":"2018-06-01 07:56:14","updated_at":"2018-06-01 07:56:14","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":5,"created_at":"2018-06-01 07:56:14","updated_at":"2018-06-01 07:56:14","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":6,"created_at":"2018-06-01 07:56:14","updated_at":"2018-06-01 07:56:14","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":7,"created_at":"2018-06-01 07:56:49","updated_at":"2018-06-01 07:56:49","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":8,"created_at":"2018-06-01 07:56:49","updated_at":"2018-06-01 07:56:49","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":9,"created_at":"2018-06-01 07:56:49","updated_at":"2018-06-01 07:56:49","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":10,"created_at":"2018-06-01 07:58:31","updated_at":"2018-06-01 07:58:31","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":11,"created_at":"2018-06-01 07:58:31","updated_at":"2018-06-01 07:58:31","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":12,"created_at":"2018-06-01 07:59:32","updated_at":"2018-06-01 07:59:32","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":13,"created_at":"2018-06-01 07:59:32","updated_at":"2018-06-01 07:59:32","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":14,"created_at":"2018-06-01 08:00:17","updated_at":"2018-06-01 08:00:17","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":15,"created_at":"2018-06-01 08:00:17","updated_at":"2018-06-01 08:00:17","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":16,"created_at":"2018-06-01 08:02:04","updated_at":"2018-06-01 08:02:04","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":17,"created_at":"2018-06-01 08:02:04","updated_at":"2018-06-01 08:02:04","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":18,"created_at":"2018-06-01 08:02:27","updated_at":"2018-06-01 08:02:27","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":19,"created_at":"2018-06-01 08:02:27","updated_at":"2018-06-01 08:02:27","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":20,"created_at":"2018-06-01 08:02:52","updated_at":"2018-06-01 08:02:52","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":21,"created_at":"2018-06-01 08:02:52","updated_at":"2018-06-01 08:02:52","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":22,"created_at":"2018-06-01 08:03:22","updated_at":"2018-06-01 08:03:22","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"},{"id":23,"created_at":"2018-06-01 08:03:22","updated_at":"2018-06-01 08:03:22","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":4,"nombre":"Nueva area","borrado":"false"}],"empresa":{"id":61,"created_at":"2018-05-31 08:07:29","updated_at":"2018-06-01 05:11:24","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"nombre":"Jose","ruc":"898987"},"puestos":[]},{"id":5,"created_at":"2018-06-01 04:59:59","updated_at":"2018-06-01 04:59:59","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Empresa_id":63,"numero_trabajadores":9,"nombre_sede":"iii","newColum":25,"area_trabajos":[{"id":24,"created_at":"2018-06-01 08:16:10","updated_at":"2018-06-01 08:16:10","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Sede_id":5,"nombre":"kkk","borrado":"false"}],"empresa":{"id":63,"created_at":"2018-06-01 02:00:48","updated_at":"2018-06-01 02:00:48","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"nombre":"Javier","ruc":"7777"},"puestos":[]},{"id":6,"created_at":"2018-06-01 05:00:39","updated_at":"2018-06-01 05:00:39","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Empresa_id":63,"numero_trabajadores":89,"nombre_sede":"iii","newColum":36,"area_trabajos":[],"empresa":{"id":63,"created_at":"2018-06-01 02:00:48","updated_at":"2018-06-01 02:00:48","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"nombre":"Javier","ruc":"7777"},"puestos":[]},{"id":7,"created_at":"2018-06-01 05:01:50","updated_at":"2018-06-01 05:01:50","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Empresa_id":63,"numero_trabajadores":9,"nombre_sede":"ppp","newColum":49,"area_trabajos":[],"empresa":{"id":63,"created_at":"2018-06-01 02:00:48","updated_at":"2018-06-01 02:00:48","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"nombre":"Javier","ruc":"7777"},"puestos":[]},{"id":8,"created_at":"2018-06-01 05:02:24","updated_at":"2018-06-01 05:02:24","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"Empresa_id":63,"numero_trabajadores":78,"nombre_sede":"oioioioioio","newColum":64,"area_trabajos":[],"empresa":{"id":63,"created_at":"2018-06-01 02:00:48","updated_at":"2018-06-01 02:00:48","deleted_at":null,"auditoria_estado":null,"auditoria_user":null,"nombre":"Javier","ruc":"7777"},"puestos":[]}]