自己加入 Laravel 5.2
Self Join in Laravel 5.2
我有以下门票Table
if(!Schema::hasTable('tblticket')) {
Schema::create('tblticket', function (Blueprint $table) {
$table->increments('TicketID');
$table->string('Subject', 50);
$table->integer('ParentTicketID')->nullable()->unsigned();
$table->timestamps();
$table->foreign('ParentTicketID')->references('TicketID')->on('tblticket');
});
}
Primary Key为TicketID,还有一列ParentTicketID,与TicketID相关
以下是门票型号
class TicketModel extends Model
{
public $table = 'tblticket';
public $primaryKey = 'TicketID';
public $timestamps = true;
public function TicketReplies() {
return $this->belongsTo('\App\Models\TicketModel', 'TicketID');
}
}
下面是我的查询
$Ticket = \App\Models\TicketModel
::with('TicketReplies')
->where('ParentTicketID', '=', $TicketID)
->first();
我正在尝试获取一张票的所有子票。但我得到了空值。
如果我遗漏了什么,你能指导一下吗?
这是我的示例代码,你可以试试这个,希望对你有所帮助
/*---------------------------------------------------------
* Relationship with same table, means recursive key
* --------------------------------------------------------
*/
//this will get the childern against parent.
public function doseage_childs(){
return $this->hasMany('App\Models\DoseageForm', 'parent_id', 'id');
}
//this will get the parent against childern
public function doseage_parent(){
return $this->belongsTo('App\Models\DoseageForm', 'parent_id', 'id');
}
已编辑
更新你的方法
public function TicketReplies() {
return $this->belongsTo('\App\Models\TicketModel', 'TicketID');
}
像这样
public function TicketReplies() {
return $this->hasMany('\App\Models\TicketModel','ParentTicketID' ,'TicketID');
}
并像这样更新您的查询模型,因为您已经获得 TicketReplies
关系。
$Ticket = \App\Models\TicketModel
::with('TicketReplies')
->where('TicketID', '=', $TicketID)
->first();
你们的关系会成功
我有以下门票Table
if(!Schema::hasTable('tblticket')) {
Schema::create('tblticket', function (Blueprint $table) {
$table->increments('TicketID');
$table->string('Subject', 50);
$table->integer('ParentTicketID')->nullable()->unsigned();
$table->timestamps();
$table->foreign('ParentTicketID')->references('TicketID')->on('tblticket');
});
}
Primary Key为TicketID,还有一列ParentTicketID,与TicketID相关
以下是门票型号
class TicketModel extends Model
{
public $table = 'tblticket';
public $primaryKey = 'TicketID';
public $timestamps = true;
public function TicketReplies() {
return $this->belongsTo('\App\Models\TicketModel', 'TicketID');
}
}
下面是我的查询
$Ticket = \App\Models\TicketModel
::with('TicketReplies')
->where('ParentTicketID', '=', $TicketID)
->first();
我正在尝试获取一张票的所有子票。但我得到了空值。
如果我遗漏了什么,你能指导一下吗?
这是我的示例代码,你可以试试这个,希望对你有所帮助
/*---------------------------------------------------------
* Relationship with same table, means recursive key
* --------------------------------------------------------
*/
//this will get the childern against parent.
public function doseage_childs(){
return $this->hasMany('App\Models\DoseageForm', 'parent_id', 'id');
}
//this will get the parent against childern
public function doseage_parent(){
return $this->belongsTo('App\Models\DoseageForm', 'parent_id', 'id');
}
已编辑
更新你的方法
public function TicketReplies() {
return $this->belongsTo('\App\Models\TicketModel', 'TicketID');
}
像这样
public function TicketReplies() {
return $this->hasMany('\App\Models\TicketModel','ParentTicketID' ,'TicketID');
}
并像这样更新您的查询模型,因为您已经获得 TicketReplies
关系。
$Ticket = \App\Models\TicketModel
::with('TicketReplies')
->where('TicketID', '=', $TicketID)
->first();
你们的关系会成功