vendredi 20 décembre 2019

Invoking Eloquent method through Reflaction Class, Method not found error

I'm invoking the modal method by ReflactionClass as given below code,

public static function get($class = 'App/Tag'){
    $modal = new ReflectionClass($class);
    if($modal->hasMethod('all')){
        $data = $modal->getMethod('all')->invoke($modal);
        return (json_encode($data));
    }else{
        throw new MethodNotFoundException();
    }
}

The above given function works fine and fetches all data using eloquent's all() method. But when i tried to fetch modal along with relationship, I faced the method withCount not found. There the error prone code.

public static function get($class = 'App/Tag' , $cnt = true){
    $modal = new ReflectionClass($class);
    if($modal->hasMethod('all') && ($cnt ? $modal->hasMethod('withCount') : true)){
        $data = $modal->getMethod('withCount')->invokeArgs($modal, ['product'])->getMethod('all')->invoke($modal);
        return (json_encode($data));
    }else{
        throw new MethodNotFoundException();
    }
}

I'm trying to fetch the modal with count of product relationship using Reflaction.





Aucun commentaire:

Enregistrer un commentaire