【发布时间】:2018-05-24 20:54:41
【问题描述】:
我有两张桌子:
**plans:**
planId, plan_name
**Users:**
userId, user_name, password, planId
我试图获取选择所有用户的计划名称。
这是用户模型:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Users extends Authenticatable {
public $timestamps = false;
protected $table = 'users';
protected $primaryKey = 'userId';
protected $fillable = [
'user_name',
'password',
];
protected $hidden = [
'_token',
];
public function plan()
{
return $this->belongsTo('App\Plans', 'planId');
}
public function validateCredentials( MyUserInterface $user, array $credentials ) {
$plain = $credentials["password"] . $user->getAuthPasswordSalt();
return $this->hasher->check( $plain, $user->getAuthPassword() );
}
}
这是计划模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Plans extends Model {
public $timestamps = false;
protected $table = 'plans';
protected $primaryKey = 'planId';
protected $fillable = [
'plan_name'
];
protected $hidden = [
'_token',
];
public function users()
{
return $this->hasMany('App\Users', 'planId');
}
}
当我使用时: \App\Users::get();
输出中没有关系...只有用户。
我能做什么?
我尝试使用 hasOne 和同样的问题...
很多
【问题讨论】:
-
你真的在使用关系吗? IE。
App\Users::with(["plan"])->get();?除非您专门调用它们,否则不包括相关模型/集合。 -
tnx 这解决了我的问题
标签: php laravel frameworks