Eduardo Rafael
Eduardo Rafael

Reputation: 81

Laravel IF Multiple values in Cookie

I have a cookie in Laravel and a variable from my server.

I want to validate the result of my server's variable with the result of the cookie.

Normally I would use this but the detail is that my server's cookie has multiple values separated by commas and inside one of them is the value I need.

@if (Cookie::get('DeliveryToken') == '{{ $orderitemaddons32 }}')
    {{ Cookie::get('DeliveryToken') }}
@else
    No se encontro
@endif

How do I get it to detect the comma-separated value? I give you an example.

My variable to compare

"{{ $orderitemaddons32 }}" value = DEL-43421
"DeliveryToken" value: delton,DEL-43421,hp-32

As you can see the exact value after the first comma I need the if to parse all the values after the comma. Or if it is in the first one to stop there and give the result of the if

Upvotes: 0

Views: 183

Answers (1)

Ben Gooding
Ben Gooding

Reputation: 1051

A simple inline method for doing this would be convert your comma separated string into an array using explode().

Then check that the value you expect is inside that array with in_array().

For example:

in_array($check_value, explode(',', $cookie))

If this is something you frequently do, you may want to create a service to put this logic and other cookie based manipulations in.

Upvotes: 1

Related Questions