gpt4 book ai didi

php - 仅从连接表中获取指定的列

转载 作者:行者123 更新时间:2023-11-29 02:19:42 24 4
gpt4 key购买 nike

首先我有这个“用户”模型

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword;

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $primaryKey = 'user_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['username', 'email', 'password'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];

public function user_details(){
return $this->hasOne('App\users_details');
}

}

和这个“users_details”模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class users_details extends Model {

protected $table = "user_details";


public function ud()
{
return $this->belongsTo('App\User');
}
}

我正在尝试仅从“user_details”模型中获取特定列(“user_details [foreign key = user_id]”模型与“User [primary key/reference key = user_id]”模型相关)

$users = User::with('user_details')->get(array('users.user_id', 'users_details.phone'));
dd(var_dump($users->toArray()));

但不幸且可悲的是,无法正常工作,我收到此错误(请参阅下文)

QueryException in Connection.php line 624: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users_details.phone' in 'field list' (SQL: select users.user_id, users_details.phone from users)

有什么想法、帮助、线索、建议、建议吗?

最佳答案

试试这个,

 User::with(array('user_details'=>function($query){
$query->select('user_id','phone');
}))->get();

希望它有用。

关于php - 仅从连接表中获取指定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com