查询构建器中的Laravel Mutator?

时间:2018-03-04 19:20:12

标签: laravel laravel-5

我不知道如何在查询构建器中使用mutator将两个字段定义为一个字段 这是我的代码

$model = LoanInformation::select( 
    'basic_information.first_name', 'basic_information.last_name',
    'loan_information.id', 'loan_information.tenor'
)->join(
    'basic_information', 'loan_information.user_id', '=', 'basic_information.user_id'
)->where(
    'loan_information.status', 'funding'
)->paginate(9);

如何将first_name和last_name定义为name并在其上添加返回strtoupper(first_name)strtoupper(last_name)

的语句

2 个答案:

答案 0 :(得分:1)

LoanInformation模型中,添加以下方法:

public function getNameAttribute() {
    return strtoupper($this->first_name . ' ' . $this->last_name);
}

这将使您能够编写$model->name,它将返回名字和姓氏,以空格连接,全部为大写。

答案 1 :(得分:0)

我只是通过向数组添加键和值而不是mutator或accessor

来弄明白

这是我自己的答案

{
"_id": {
    "$oid": "5a99553c1b4a1b0922039e73"
},
"date_added": {
    "$date": "2018-03-02T13:42:36.684Z"
},
"title": "Black Panther",
"__v": 14,
"alls": [
    {
        "name": "Mbezi",
        "geocoordinates": "-6.8140361,39.2775983",
        "region": "Dar es salaam",
        "times": "",
        "_id": {
            "$oid": "5a9c74885980731a35a013b2"
        }
    },
    {
        "name": "DFM",
        "geocoordinates": "-6.7726935,39.2196418",
        "region": "Dar es salaam",
        "times": "",
        "_id": {
            "$oid": "5a9c7f61bb57291a81d8309e"
        }
    }
]