skip to Main Content

Hello I’ve a model that is additionalInfo for a user, so if the user fills this additional info it will get a reward, so I’ll need to check if all values from this additional info is filled. My question is, do I need to check it per every value? I mean do I need to check it like this per attribute using $clientAdditionalInfo['adress'] then I will check the next $clientAdditionalInfo['city'] then $clientAdditionalInfo['province'] etc…

Or should I do something like this.

public function isCompleted() {
    $user = auth()->user();
    
            $clientAdditionalInfo = $user->client->additionalClientInfo->where('client_id', $user->client->id)->get()->first();
    
    if($clientAdditionalInfo){
                $attributes = $clientAdditionalInfo->getAttributes();
    
                foreach($attributes as $attribute) {
                    // HERE I COULD CHECK IF EVERY ATTRIBUTE IS FILLED?
                }
            }
}

Tried iterating per attribute and maybe I should check per attribute if isset and then if all are set is that all additionalInfo is set and give the reward. But once all attributes are set how can I check that ALL attributes are set?

What I’m seeing if when I loopon the attributes, there’s a last value which is null (after the updated_at) but on the database there’s no more values after the database, it could be a thing of the foreach? There’s only one row on the database and filled all the values, if I dump($key) & dump($attribute) on the foreach I get the following:

^ "id"
^ 1
^ "address"
^ "Calle test 123"
^ "city"
^ "Barcelona"
^ "province"
^ "Barcelona"
^ "postal_code"
^ "08010"
^ "web"
^ "https://www.test.com"
^ "phone"
^ "123456789"
^ "activity"
^ "Social"
^ "number_of_employees"
^ 150
^ "interest"
^ "Test"
^ "client_id"
^ 8
^ "created_at"
^ "2023-02-08 10:48:56"
^ "updated_at"
^ "2023-02-08 11:53:43"
^ null

I dont know where this null is coming from, it shouldn’t…

Okay my bad, I was checking it badm I should check it from $request->all(). This way I’ll be able to check just the values without the created_at & updated_at so I can be more specific and check it better.

2

Answers


  1. The simplest approach is to have a loop like you suggested in the question:

    $attributes = $clientAdditionalInfo->getAttributes();
    foreach ($attributes as $key => $value) {
        //Check whether the attribute is set properly
    }
    

    Yet, this is not always easy. Sometimes you have a default value for an attribute that’s not null and which is set as a default. For example, you might have values such as 'NOTSET', 'UNINITIALIZED', 'INITIALIZED', 'FINALIZED'. In such cases you need to check whether the value is 'NOTSET'. Also, your attribute might default to 0 whereas proper values are strictly positive (prices, for example). So, you might have further functions/criterias to evaluate the different kinds of attributes you may have.

    Also, it’s quite possible that you have a null value somewhere, but it’s perfectly fine and properly initialized. For example, a date of death is unknown, i.e. null for people who are currently alive.

    Furthermore, it’s quite possible that you have a composite attribute, like some items which is an array of items and each item has its own attributes, in which case you will need to loop for them, so it makes sense to implement a function for iterable collections such as arrays or StdClass objects.

    Finally, you may have some object instances which need to be specially evaluated as attributes of additional info. For example, if health status is part of additional info, health status might be an object on its own right.

    You can json_encode such objects and then json_decode into an array and handle it as an attribute array (nested or not) in your evaluation. Or, you could create an array of expected attributes for your class and loop that. The latter has the advantage of allowing some attributes to be uninitialized if you wish, but, the disadvantage is that you will need to keep an eye on that and possibly adjust it. You can do that semi-automatically as well, by tracking new attributes upon each build/deploy and assume the new attributes to be required.

    But, returning to the simplest approach, it looks like this:

    foreach ($attributes as $attribute) {
        if ((!isset($yourObject->{$attribute}))) {
            return false; //something was not initialized
        }
    }
    return true; //we reached this point after looping everything and finding everything being initialized
    

    The above was not tested, you may need to perform it in a slightly different way in your case, depending on how your objects and collections look alike, but the idea is to loop the attributes, see for each of them whether they are properly set, return false if not. Once everything was checked and found to be initialized properly, return true.

    Login or Signup to reply.
  2. Use array_filter

    if(!array_diff($attributes, array_filter($attributes))) {
       echo 'All attributes are filled';
    }
    

    If no callback is supplied, all entries of array equal to FALSE deleted.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search