mercredi 9 mars 2016

Self Join in Laravel 5.2

I have the following Ticket 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 is TicketID and There is another column called ParentTicketID, which is related to TicketID.

Below is Ticket Model

class TicketModel extends Model
{
    public $table = 'tblticket';
    public $primaryKey = 'TicketID';
    public $timestamps = true;

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

Below is my Query

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

I am trying to get all child tickets of a Ticket. but I am getting null.

Can you please guide if I am missing something.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire