Skip to content

Instantly share code, notes, and snippets.

@singleseeker
Last active October 12, 2015 06:02
Show Gist options
  • Save singleseeker/597be5fd01e3a10cad8a to your computer and use it in GitHub Desktop.
Save singleseeker/597be5fd01e3a10cad8a to your computer and use it in GitHub Desktop.
eloquent 用法
// ======================== Company.php ==========
class Company extends Model
{
protected $table = 'tbl_company';
protected $primaryKey = 'com_id';
public function company_invest_events()
{
return $this->hasMany('App\Models\Company_invest_events','invse_com_id','com_id')->with('invse_orags_list');
}
}
// ======================== Company_invest_events.php ==========
class Company_invest_events extends Model
{
protected $table = 'tbl_investevent';
protected $primaryKey = 'invse_id';
public function invse_orags_list()
{
// 这个地方怎么写
return $this->belongsToMany('App\Models\Company_invest_events_orags_list','tbl_rel_invse_with_invsp_or_invst','invse_with_invse_id','invse_with_src_id');
}
}
company.php, 一对多,通过通找到invest_events的列表,并要求引入invse_orags_list字段。
但 tbl_rel_invse_with_invsp_or_invst 段是这样子的
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| invse_with_id | int(11) | NO | PRI | NULL | auto_increment |
| invse_with_invse_id | int(11) | YES | MUL | NULL | |
| invse_with_src_id | int(11) | YES | | NULL | |
| invse_with_src_type | varchar(45) | YES | | NULL | |
| migrate_id | varchar(100) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
本身是个多对多的,但是要根据 invse_with_src_type 来区分,
* 如果type是 'invst' , invse_with_src_id 就会与 A 表关联
* 如果type是 'invsp' , invse_with_src_id 就会与 B 表关联
这种要如何来应用 eloquent?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment