skip to Main Content

I have User model when I tried to return all users like this query

 $users = User::all();

    $userCollections = (object)collect([]);
    foreach ($users as $user)
    {
        $userCollections['name'] = $user->name,
        $userCollections['date'] = $user->created_at; // here working fine in local but in server
//returning null array like this [] and its in database like this -> 2020-06-30 13:11:01
        ...
        ..
        etc ...
    }

That’s work fine, but when I return this from ubuntu server – the response will be like this

"created_at": [],

Also, if I tried to return Carbon::now() the server return null array like this [] and in local working fine.

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem by changing the php version from php7.4 into php7.2


  2. Try this:

    $userCollections = User::all()->mapWithKeys(function ($user) {
        return [['name' => $user->name, 'date' => $user->created_at]];
    })
    

    Laravel mapWithKeys

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