Self Join in Laravel 5.2

后端 未结 1 1522
时光说笑
时光说笑 2021-01-23 13:01

I have the following Ticket Table

if(!Schema::hasTable(\'tblticket\')) {
    Schema::create(\'tblticket\', function (Blueprint $table) {
                


        
1条回答
  •  清歌不尽
    2021-01-23 13:14

    this is my sample code, you can try this, I hope that will help you

    /*---------------------------------------------------------
     * 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');
    }
    

    Edited

    update your this method

    public function TicketReplies() {
        return $this->belongsTo('\App\Models\TicketModel', 'TicketID');
    }
    

    like this

    public function TicketReplies() {
        return $this->hasMany('\App\Models\TicketModel','ParentTicketID' ,'TicketID');
    }
    

    and update your query model like this, because you already getting TicketReplies relationships.

    $Ticket = \App\Models\TicketModel
        ::with('TicketReplies')
        ->where('TicketID', '=', $TicketID)
        ->first();
    

    You relationship will works then

    0 讨论(0)
提交回复
热议问题