[英]get only specified columns from the join table
首先我有這個“用戶”模型
<?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()));
但不幸的是,可悲的是,無法正常工作,我收到此錯誤(請參閱下文)
Connection.php 第 624 行中的 QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users_details.phone' in 'field list' (SQL: select
users
.user_id
,users_details
.phone
fromusers
)
任何想法,幫助,線索,建議,推薦?
嘗試這個,
User::with(array('user_details'=>function($query){
$query->select('user_id','phone');
}))->get();
希望它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.