Jamie Jamie - 10 months ago 109
PHP Question

Laravel sync not working with empty array

I've a method looks like this:

public function saveContacts(Request $request)
if($request->contacts) {
$contacts = collect($request->contacts)->pluck('id');

It's working but if
is an empty array it does not remove all records. What could be the case here?

Answer Source

You can do this:

if ($request->contacts) {
    $contacts = collect($request->contacts)->pluck('id')->toArray();
    if (empty($contacts)) {
    } else {