Welcome Guest, Not a member yet? Register   Sign In
No matching query: return empty key values instead null. Is this possible?
#1

(This post was last modified: 05-24-2020, 12:01 PM by YanKleber.)

Let's take this simple statement:

PHP Code:
$myarray $this->asArray()->where('id',$id)->first(); 

If it finds a record it will return a collection of key/value pairs.

I'd like to know if there is a way to make it return a collection of keys with empty values when it doesn't find a matching record instead of return null.

Thanks!



I figured it out. Not as straight forward as I wanted but it works finely. Here it goes in case someone else needs it:


PHP Code:
public static function getEmptyRecord($table) {
    $db = \Config\Database::connect();
    $builder $db->query("SHOW COLUMNS FROM $table");
    $result = [];
    foreach (json_decode(json_encode($builder->getResult()),true) as $row)
      $result[$row['Field']] = '';
    return $result;
  

Cool
Recovering the wasted time...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB