如何遍历对象并在关联数组中显示其数据

First of all this is my code:

Log::create([

    'action' => $event->changes[0], //<- This is my problem

    'object_id' => $event->id,
    'object_type' => "Account",
    'ip_address' => Request::ip(),
    'user' => ucfirst(Auth::user()->name),
    'time' => Carbon::now()->format('H:i:s'),
    'date' => Carbon::now()->format('Y-m-d')
]);

$event->changes is an array which contains many items but i only know how to get 1 specific item at a time using the index [0] or [1] etc.

How do I get all the values to display instead of 1 at a time? Obviously I don't want to create a new log for each single action but I cant figure out how to do this.

As always any help is appreciated thank you.

You need to serialize the $event->changes if you want it within one log entry.

It actually depends on the structure of changes array, but it seems to be an array of strings, so you may for example use the implode(', ', $changes), so the snippet would look the following:

Log::create([

    'action' => implode(', ', $event->changes), //<- This is my problem

    'object_id' => $event->id,
    'object_type' => "Account",
    'ip_address' => Request::ip(),
    'user' => ucfirst(Auth::user()->name),
    'time' => Carbon::now()->format('H:i:s'),
    'date' => Carbon::now()->format('Y-m-d')
]);