我有两张这样的表:
users: users_id | name | users_roles_id
users_roles: users_roles_id | roles
我想加入这些表格。我尝试过以下方法:
use Elegant\Model as Eloquent;
class User extends Eloquent {
protected $table = 'users';
function getRoles($userId){
User::join('users_roles', 'users_roles.users_roles_id', '=', 'users.users_roles_id')
->select('*')
->get();
}
public function user_role() {
return $this->belongsTo('users_roles','users_roles_id');
}
}
但我不知道如何加入这些来展示它。 感谢
答案 0 :(得分:0)
尝试替换此
object?
使用
return $this->belongsTo('users_roles', 'users_roles_id');
阅读更多Eloquent Laravel文件,名称方便
答案 1 :(得分:0)
我做了
class User extends Eloquent {
protected $table = 'users';
function getRoles($userId){
return $this->where('id', $userId)->join('users_roles', 'users.users_roles_id = users_roles.users_roles_id')->get();
}
}
它正在运作; - )
答案 2 :(得分:0)
您创建
芯/ MY_Model.php
<?php
require_once FCPATH . 'filelocation/Eloquent.php';
class MY_Model extends Eloquent {
public function __construct() {
parent::__construct();
}
}
然后在模型上
模型&gt; User_model.php
<?php
class User_model extends MY_Model {
public function __construct() {
parent::__construct();
}
}