1

Hope one of you can help me with a problem.

I get an error, telling me i'm not using an array with in_array().

Using pluck() should get me an array, right?

{{ Request::ip() }}
{{ $pug->ipbans->pluck('ip') }}

Output:
127.0.0.1
["127.0.0.1","127.0.0.1"]

Then in Blade:

@if( in_array( Request::ip(), $pug->ipbans->pluck('ip')  ) )
    <div class="alert alert-danger">
        Your IP has been blocked on this page. Changes not allowed.
    </div>
@endif

I'm getting the following error:

in_array() expects parameter 2 to be array, object given (View: ...)

So it tells me that $pug->ipbans->pluck('ip') is not an array - but it is, right?

2
  • 1
    just add toArray() $pug->ipbans->pluck('ip')->toArray(); Commented Aug 9, 2020 at 11:13
  • Thanks! That fixed it. So a collection is not an array? I thought it was. Post an answer so I can accept it :) Commented Aug 9, 2020 at 11:15

3 Answers 3

2

according to laravel doc

the collection method 'pluck' return a collection not an array

to make the result an array you can use 'all':

{{ $pug->ipbans->pluck('ip')->all() }}
Sign up to request clarification or add additional context in comments.

Comments

2

Try like this

$pug->ipbans->pluck('ip')->toArray();

Comments

1

You only need to add toArray()

 $pug->ipbans->pluck('ip')->toArray(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.