2

I am returning all shipping address saved by user but there are some values which are left blank and output comes as null in those. How can i convert NULL values to blank (" "). I have looked many solutions on internet but not able to get make it working. API link:

https://androidapp.factory2homes.com/api/shippings/3
$address = DB::table('shippings')->where('user_id' , $user_id)->get();
  return $address;
1
  • Maybe you can show us your "many solutions", and tell us why they don't work? For now: make your address a true eloquent model, for example model Shipping. Return that model in a response, either by simply returning the model or by returning a model resource. Either way, internally laravel calls several functions to get the attribute values. You have the ability to access these attributes, turning null values into blanks. See laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor Commented Jul 19, 2020 at 18:24

1 Answer 1

1

You can just map the return value like:

return $address = DB::table('shippings')
                    ->where('user_id' , $user_id)
                    ->get()
                    ->map(function ($item) {
                       $mapped = [];

                       foreach ($item as $key => $value) {
                           $mapped[$key] = $value ?? ' ';
                       }
                       
                       return $mapped
                    });
Sign up to request clarification or add additional context in comments.

Comments

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.