Check if the Object request is empty before for each

Published 1 week ago by Bribin

<tr >
<td> {!! Form::text('contacts[][first_name]',NULL, ['class' => 'form-control'] ) !!}</td>
<td> {!! Form::text('contacts[][last_name]',NULL, ['class' => 'form-control'] ) !!}</td>
<td> {!! Form::text('contacts[][contact_email]',NULL, ['class' => 'form-control'] ) !!}</td>
<td> {!! Form::text('contacts[][contact_phone]',NULL, ['class' => 'form-control'] ) !!}</td>
<td> {!! Form::text('contacts[][contact_department]',NULL, ['class' => 'form-control'] ) !!}</td>
 </tr>

check if the Object request is empty before foreach, without form validation


if(empty($request->contacts))
{
 do something
}

kreierson

If you pass a contacts variable from your controller to your view like this:

$contacts = Contacts::all();
return view('contacts', compact('contacts'));

Then you can do this:

@if(!$contacts->isEmpty())
@foreach($contacts as $contact)
<tr >
        <td> {!! Form::text('contacts[][first_name]',NULL, ['class' => 'form-control'] ) !!}        </td>
        <td> {!! Form::text('contacts[][last_name]',NULL, ['class' => 'form-control'] ) !!}</td>
        <td> {!! Form::text('contacts[][contact_email]',NULL, ['class' => 'form-control'] ) !!}</td>
        <td> {!! Form::text('contacts[][contact_phone]',NULL, ['class' => 'form-control'] ) !!}</td>
        <td> {!! Form::text('contacts[][contact_department]',NULL, ['class' => 'form-control'] ) !!}</td>
 </tr>
@endforeach
@endif
Bribin
Bribin
1 week ago (3,185 XP)

I want to insert the data into database not displaying

kreierson

Convert the contacts array to a collection:

$contacts = collect($request->contacts);

// Then either do this

$contacts->each(function($contact) {
    // Do Something
});

// or this

if(!$contacts->isEmpty()) {
    // Do Something
} 

// or this

if($contacts->count()) {
      // Do Something
}

Snapey
Snapey
1 week ago (779,535 XP)

you need to be clearer please about what you need help with

Bribin
Bribin
1 week ago (3,185 XP)

i tried this. its not working with my needs.

$contacts = collect($request->contacts);

Output is

[{"first_name":null,"last_name":null,"contact_email":null,"contact_phone":null,"contact_department":null}]

if the output values is null i dnt want to save the values. otherwise save the values

Snapey
Snapey
1 week ago (779,535 XP)

if just one of them is null, all of them null, some null and some set?

what are the rules?

Bribin
Bribin
1 week ago (3,185 XP)

All of them is null

Snapey
Snapey
1 week ago (779,535 XP)

unfortunately there is no way other than a large if statement, checking each field in turn

you could simplify a little by imploding all the fields (concatenates them all as one string) and then just test that.

disljenko
      $validator = \Validator::make($request->all(), [

            'contacts.*' => 'required',
        ]);


        if ($validator->fails()) {

            $response->flag = false;
            $response->fail = $validator->errors();
            return response()->json($response);

        }
Cronix
Cronix
1 week ago (185,660 XP)

You can try this:

// Get all contacts from request
$contacts = collect($request->contacts);

// Keep only fields from $contacts that have a null value
$filtered = $contacts->filter(function($value, $key) {
    return is_null($value);
});

// compare counts
if ($contacts->count() === $filtered->count())
{

    // all fields in $contacts have a value of NULL
} else {

    // none or not all fields are null
}
vincent4

$contacts = [ 'first_name' => NULL ];

$contacts['first_name'] == NULL) ; // true

vincent4

of course if($requerst->contacts->first_name == NULL){ do something }

Snapey
Snapey
1 week ago (779,535 XP)

I wrote a little test;

    //setup test data - real data will come from request
        $address = [
                    "first_name" => null,
                    "last_name" =>null,
                    "contact_email" => null,
                    "contact_phone" => null,
                    "contact_department" => null,
        ];


    //imploding the array creates a string, null if all fields are empty

        if(empty(implode($address))) {

            dd('all fields are null');

        } else {

            dd('at least one of the fields is populated');

        }
biishmar

This check the key request present or not

$request->has('contacts')

This checks the contacts empty or not

$request->filled('contacts')
Bribin
Bribin
1 week ago (3,185 XP)

It's not a single key.its a request array having 3 fields

Please sign in or create an account to participate in this conversation.