2

Why does this code return false every time?

$lics = collect(['lic100' => auth()->user()->lic100, 'lic250' => auth()->user()->lic250, 'lic500' => auth()->user()->lic500]);
$licsowned = $lics->filter()->keys();
$haslicense = property_exists($licsowned, $data['lictype']);

$licsowned:

Illuminate\Support\Collection {#367 ▼
 #items: array:3 [▼
  0 => "lic100"
  1 => "lic250"
  2 => "lic500"
 ]
}

$data['lictype'] has the value lic250

I also tried with in_array() but it gave the error message that the value must be an array and I passed an object.

4
  • 1
    So, did you try the contains() method of Collection instance? laravel.com/docs/7.x/collections#method-contains Commented Apr 20, 2020 at 19:47
  • 1
    I did it, man. :) Commented Apr 20, 2020 at 19:50
  • @OlegNurutdinov please elaborate a little bit the answer, else i have to accept the other one since it contains more informations and an example. thanks. Commented Apr 20, 2020 at 19:52
  • 1
    Updated it. Should bo better than was. Commented Apr 20, 2020 at 19:56

2 Answers 2

7

You need to use contains() method of Collection instance.

For example:

$collection = collect(['name' => 'Desk', 'price' => 100]);

$collection->contains('Desk');

Or, in your task:

$lics = collect(['lic100' => auth()->user()->lic100, 'lic250' => auth()->user()->lic250, 'lic500' => auth()->user()->lic500]);

$lics->contains('lic250');

More info:

https://laravel.com/docs/7.x/collections#method-contains

Sign up to request clarification or add additional context in comments.

Comments

3

Your $licsowned variable is a collection. You can use contains() method. Example:

$licsowned->contains('lic100')

More information about contains(): https://laravel.com/docs/7.x/collections#method-contains

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.