1

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?

1 Answer 1

2

You are using normal OOP functionality here, has nothing to do with the Laravel relationship. So in order to call your experience_level() function, just use:

$skill->experience_level()

I hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

I know I'm using the normal OOP, and I know I can use it like that but I'm trying to see if there is a way to use it as a relation! I'll wait for answers before accepting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.