1

I have in my model a query to get data from the database and there are nullable fields present in my database table. When I try to display all the records in my view, the fields which are of null value, is displayed as "NULL". I want these values to be display as just empty string.

Here is what I have in my controller:

public function showDetails($action, $id)
{
        $data_model= new PersonData();
        $persons= $data_model->loadSpecificPerson($id);

        return view('pages.admin.reporting_person_details', ['action' => $action, 'id' => $id])->with('id', $id)->with(compact('persons'));
}

In my model:

public function loadSpecificPerson($id)
{
        $persons = DB::select("SELECT * FROM complainant_t WHERE ComplainantID = ?", [$id]);
        return $persons;
}

I wanted to do it like how you would do it in an array:

foreach($array as $key => $value)
{
   if($array[$key] == NULL)
   {
        $array[$key] = "";
   }
}

This isn't possible to use with $persons since it's an object and must be accessed with name right? So how should I perform this without having to access it by the column names?

5
  • 1
    Are you sure the value is NULL and not 'NULL` in your DB? Two different things Commented Feb 22, 2017 at 6:18
  • $result = DB::select($sql)->get(); $result = $result->toArray(); it will convert your object to an array Commented Feb 22, 2017 at 6:19
  • Supposedly, when it is NULL, it won't show anything Commented Feb 22, 2017 at 6:21
  • @geckob in my Database, NULL is the default value. Commented Feb 22, 2017 at 7:14
  • @FriencyFernandez You figured out Commented Feb 22, 2017 at 7:46

3 Answers 3

1

Since I'm not the one who made the DB, I've just realized the problem is because the field values are "NULL" (string). I've modified it to be NULL and it is working fine now without having to convert it to empty string.

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

2 Comments

Exactly. A lot of people confused about this
@geckob Yes it was a silly mistake and I did not realize it until I looked at the database. Thank you for pointing it out!
0

Basically NULL field is always displayed '' empty string. But if not then you can achieve as below.

foreach($persons as $person){
    //consider you want to display here
    //Please check it's NULL or 'NULL'
    echo $person->field === NULL ? '' : $person->field;
}

Here basically it will check all fields, If field is NULL then it will display '' empty string else it will display original value.

Hope it helps you!

2 Comments

Hi do I have to specify field?
change field according to your database column name
0

Use is_null or === operator.

  • is_null($result['column'])
  • $result['column'] === NULL

In Laravel, you can read about `toArray()' that will convert object to array and later you can use normal logic. Ref: https://laravel.com/docs/5.2/collections

In Model

   return $persons->toArray();

Logic

$object = new stdClass();
foreach($array as $key => $value)
{
    $array[$key] = is_null($value) ? '' : $value;
    $object->$key = is_null($value) ? '' : $value;
}

If you want to use object then use $object variable else $array as array

6 Comments

If i convert it to array I'd have to use array for my blade instead of object right?
Yes....and if you want to use object only in blade then convert to array and manipulate the values and again convert to object...
I don't know why but it shows an error Call to a member function toArray() on array
Please refer laravel collections link to the usage of toArray function...
|

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.