Using in_array in collections

I need to check the name in an array of names, but I'm having trouble passing the array instead of the collection to the in_array () method.

My ice code looks something like this.

@foreach($ecn->areas as $area)
{{ $area->area }}:<br>
<ul>
    @foreach($area->people as $person)                              
        @if(in_array($person, $ecn->signatures->name ))
        <li><del>{{ $person }}</del></li>
        @else
        <li>{{ $person }}</li>
        @endif
    @endforeach
</ul>
@endforeach

I know that my problem is how I try to access the list of signatures.

@if(in_array($person, $ecn->signatures->name ))

I can access them in another part of the page by doing this

@foreach($ecn->signatures as $signature)
{{ $signature->name }}<br>
@endforeach

and everything is in order.

How can I access and pass the list of signatures as an array in this scenario?

+4
source share
3 answers

lists pluck , . all(), .

foreach, .

:

$names = $ecn->signatures->lists('name')->all();

:

@if(in_array($person, $names))
+4

in_array() Laravel, :

$collection->contains($needle)

, in_array.

+2

Laravel

get , PHP StdClass.

, stdClass .

foreach ($ecn->signatures as $signature) 
     $nameArray[] = $signature->name;

in_array

@if(in_array($person, $nameArray))
+1

All Articles