Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用“;带();功能在拉威尔雄辩_Php_Sql_Select_Laravel_Eloquent - Fatal编程技术网

Php 使用“;带();功能在拉威尔雄辩

Php 使用“;带();功能在拉威尔雄辩,php,sql,select,laravel,eloquent,Php,Sql,Select,Laravel,Eloquent,我试图将不同表中的两列合并到一列中 $user = User::with(array('Person'=>function($query){ $query->selectRaw('CONCAT(prefix_person.name, " - ", prefix_user.code) as name, prefix_user.id'); }))->lists('name', 'id'); 在我的person类中我有以下方法: public function User(

我试图将不同表中的两列合并到一列中

$user = User::with(array('Person'=>function($query){
    $query->selectRaw('CONCAT(prefix_person.name, " - ", prefix_user.code) as name, prefix_user.id');
}))->lists('name', 'id');

在我的
person类中
我有以下方法:

public function User()
{
    return $this->hasOne('User');
}
在我的
用户类中,我有一个:

public function Person()
{
    return $this->belongsTo('Person', 'person_id');
}

我得到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list' (SQL: select `name`, `id` from `prefix_user`)

当我试着

$user = User::with(array('Person'=>function($query){
    $query->selectRaw('CONCAT(prefix_person.name, " - ", prefix_user.code) as name, prefix_user.id')->lists('name', 'id');
}));
我得到了这个错误:



我已经使用了好几次,但从未将其用于with(join)。

问题是,Eloquent将首先查询users表,然后才查询persons表,因此一个查询不知道另一个查询,因此连接将不起作用

您可以使用
连接来执行此操作。它将是这样的:

$user = DB::table('users as u')
    ->join('persons as p', 'p.id', '=', 'u.person_id')
    ->selectRaw('CONCAT(p.name, " - ", u.code) as concatname, u.id')
    ->lists('concatname', 'u.id');

编辑: 而且,正如@michel ayres评论所建议的,只要你有一名该领域的专家:

public function getFullNameAttribute() { 
    return $this->attributes['name'] . ' - ' . $this->attributes['code'];
}
您可以使用自己的模型执行联接和列出:

User::join('person','person.id','=','user.person_id')
    ->select('person.name', 'user.code', 'user.id')
    ->get()
    ->lists('full_name', 'id');

您只需使用简单的查询即可解决

User::join('persons as p', 'p.id', '=', 'users.person_id')
       ->get([
            'id',
            DB::raw('CONCAT(p.name,"-",users.code) as name')
        ])
       ->lists('name', 'id');
或者,换一种方式

User::join('persons as p', 'p.id', '=', 'users.person_id')
      ->select(
          'id',
          DB::raw('CONCAT(p.name,"-",users.code) as name')

        )
       ->lists('name', 'id');

你就不能用一个能把它弄脏的瓶子吗?这将使您的逻辑更易于理解和扩展。@Ravan acessor很不错,但我不知道它们如何处理
列表
和联接。你能举个例子吗?哦,我现在明白了,让我想一想。是的,你找到了我问题的根源:/我按照你的建议,在我的班里创建了一个acessor,如下所示。我仍然认为这有点难看(如果这项工作和linq一样,那么它调用2x查询),但至少使用了雄辩的。这里是最后一个:User::join('person','person.id','=','User.person_id')->orderBy('person.name','ASC')->select('person.name','User.code','User.id')->get()->list('full_name','id');在这个模型中,我有一个方法
公共函数getFullNameAttribute(){return$this->attributes['name'].-'.$this->attributes['code'];}
如果可能的话,在答案中添加(不要更改)注释=)thanks@MichelAyres我想您可以完全摆脱
->get()
。你能测试一下它是否正确,以便我更新答案吗?我测试时没有使用
->get()
,但出现了一个错误,说我没有
全名。我认为需要它来填充
函数getFullNameAttribute
,这样我就可以在
列表中使用它了。