Jamie Jamie - 1 year ago 207
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 {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download