Laravel ::提取多列 [英] Laravel ::pluck multiple columns

查看:322
本文介绍了Laravel ::提取多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要填充刀片格式<select>标签.

I need to populate a blade format <select> tag.

我知道Model::pluck('column1', 'column2')填充选择标签的方法.

I'm aware of the Model::pluck('column1', 'column2') method to populate a select tag.

但是对于我来说,我必须将两列连接起来并获取ID.像

But in my case, I have to concatenate two columns , and get the id. something like

Model::pluck('column_1 && column_2', 'id')

有可能那样吗?如果是,正确的语法是什么?

Is it possible like that ? If yes, what would be the proper syntax?

如果没有,那么什么是最好的选择?

If not, what would be the best alternative then ?

推荐答案

最好的解决方案是创建访问器函数添加到模型中,让我们假设您想获得全名,那么您可以这样做.

Best solution is to create accessor function into your model, let's assume if you want to get full name then you can do like this.

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

您可以轻松获得全名.

$users = User::where('id', 1)->get()->pluck('full_name', 'id');

Eloquent将调用getFullNameAttribute函数并获取串联值.

Eloquent will call getFullNameAttribute function and get concatenated value.

这篇关于Laravel ::提取多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆