<?php

namespace WoWPress\Models;

use LogicException;
use Wenprise\Eloquent\Model;

class CharacterListItem extends Model{

    protected $table = "wowpress_charlists_item";
    public $timestamps = false;
    protected $primaryKey = 'ID';
    protected $guarded = ['ID'];
    

    public function list(){
        $this->belongsTo(CharacterList::class,'list_id');
    }

    public function character(){
        $this->belongsTo(Character::class,'character_id');
    }

    public static function deleteOrphans(){
        $items = static::all();
        foreach($items as $item){
            try{
                $item->list;
            }catch(LogicException $e){
                $item->delete();
            }
        }
    }

}