vendredi 17 mai 2019

how to use relations to check if value exists in an array in laravel

I'm trying to use laravel relations to fetch a value.

I have declared some constants in a model Skill and trying to return if the id exists in the array of those constants or not. here is what I have

class Skill extends Model {
   CONST LEVEL_BEGINNER              =  1;
   CONST LEVEL_INTERMEDIATE          =  2;
   CONST LEVEL_EXPERIENCED           =  3;
   CONST LEVEL_EXPERT                =  4;
   public static function getLevels () {

        return [
            self::LEVEL_BEGINNER         => "Beginner",
            self::LEVEL_INTERMEDIATE     => "Intermediate",
            self::LEVEL_EXPERIENCED      => "Experienced",
            self::LEVEL_EXPERT           => "Expert",
        ];

    }
    public function experience_level () {

        return self::getLevels()[$this->experience_level_id] ?? "";

    }
}

But, when I do this in controller

$skill->experience_level

it says

App\Skill::experience_level must return a relationship instance.

How can I use this or is there any other approach to achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire