I need to check for a name in an array of names but I having trouble passing an array instead of a collection to the in_array() method.
My blade 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 my problem is in the way im trying to access the list of signatures.
@if(in_array($person, $ecn->signatures->name ))
I can access them in another part of the page doing this
@foreach($ecn->signatures as $signature)
{{ $signature->name }}<br>
@endforeach
and everything is fine.
How can I access and pass the list of signatures as an array in this scenario?
The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
If you want to use in_array()
version for Laravel Collections then you can use:
$collection->contains($needle)
It woks just like in_array
.
If $needle
is an integer, an id
for example, don't forget to pluck first, like so:
$collection->pluck('id')->contains($needle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With