princeoo7's avatar

Cannot Construct an instance of Status using the value (NULL)

I am using the Laravel Enum package and its working fine for most places. but for below code from product model I am facing below error. I have checked the db and all records have one category.


public function getCategoryAttribute () {
        return $this->category()->select('title')->get();
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }    


"message": "Cannot construct an instance of Status using the value (NULL) ``. Possible values are [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].",
    "exception": "BenSampo\Enum\Exceptions\InvalidEnumMemberException",
0 likes
6 replies
bugsysha's avatar

Why do you need whole package to enforce a simple rule/strategy?

bugsysha's avatar

I'm saying that I would not depend on a package for this kind of convenience.

bugsysha's avatar

Consider the following and tell me which is easier to write, more readable, more descriptive and expressive:

// from enum package example
$model->status = StatusEnum::DRAFT();
$model->status->isEqual(StatusEnum::ARCHIVED());
// your potential code
$model->changeStatusToDraft();
$model->makeDraft();

$model->hasArchivedStatus();
$model->isStatusArchived(); // or statusIsArchived()
$model->isArchived();
princeoo7's avatar

@bugsysha return $q->where('status_id', Status::Active); is simple for me as I can also access the label value in the api reducing the task ok maintaining the label on all frontend platforms. plus make changes at one place and update at all place sounds more good to me atleast.

bugsysha's avatar

return $q->where('status_id', Status::Active);

Doing this in more than one place is already a problem. You are breaking encapsulation which is a core OOP. You are breaking SRP which is a fundamental guideline for good code. You are allowing your code to depend on a package that can be discontinued at any time. That is just to name a few issues.

It is good that you reuse your constants, but I still do not see any advantage of using a package like that for your app. Everything can be achieved with little to none complexity and dependencies on external packages.

Please or to participate in this conversation.