Bugs on has()

Published 3 weeks ago by pramanadiputra

I think there's a bug on has()

I have these following rows.

LESSONS TABLE | ID | TITLE | .... | ..... |

(Belong to Course A)

  1. Setup Localhost
  2. Download WordPress
  3. Install WordPress
  4. WordPress Structure Directory
  5. WordPress Plugin
  6. WordPress Theme

(Belong to Course B)

  1. Setup RoR
  2. ........
  3. ........

When I'm running the following script

dd($course->lessons->has($lesson->id));

In the following URL: app.tld/course-a/lecture/{id}

When I'm accessing ID from 1 to 5, it says TRUE, however when I'm accessing ID 6, it says FALSE. It suppose to says TRUE because ID 6 is belong to Course A, but it doesn't.

Thoughts everyone?

Best Answer (As Selected By pramanadiputra)
Nash

@pramanadiputra $course->lessons gives you a collection of items. $course->lessons->has(1) will check for index 1, not if the lesson has ID 1. If you have 6 items, the highest index will be 5, because array index starts at 0. Check the output of dd($course->lessons); or see if $course->lessons->has(0) returns true.

nikos
nikos
3 weeks ago (6,420 XP)

Take a look at querying relationship existence at docs https://laravel.com/docs/5.7/eloquent-relationships#querying-relations And I'm not quite sure what are you up to? Do you want to get all the lessons from one courseor a collection of courses with its lessions ?

pramanadiputra
Snapey
Snapey
3 weeks ago (1,040,245 XP)

After the millions of uses of the framework, its you, not a bug

nikos
nikos
3 weeks ago (6,420 XP)

So you wanted to restrict users only to the courses he has access to. The 'Laravel' way of doing this is with Policies https://laravel.com/docs/5.6/authorization#creating-policies But also you can do that in your controller/repository, just by generating the right query

pramanadiputra

@Snapey

Wow what a reply, Thank You! If Laracasts has downvote system I'd already downvoted you.

@nikos

Yes, but by using this script is pretty convenient.

$course->lessons->has($lesson->id)

Thank You for pointing out Policies

Nash
Nash
3 weeks ago (72,030 XP)

has() is supposed to determine whether a key exists in a collection or a relationship exists in Eloquent. It is not meant to check if a certain ID exists.

See the docs:

pramanadiputra

@Nash

Yes, But in this case Lesson ID is becoming the key whether it exists in a collection (course->lectures). And I use it to check if a certain ID exists.

Snapey
Snapey
3 weeks ago (1,040,245 XP)

Thats alright. I have already downvoted you for a question containing no useful information.

pramanadiputra

Thats alright. A Question doesn't need to provide useful information, but Answer does, and your answer containing no useful information.

Nash
Nash
3 weeks ago (72,030 XP)

@pramanadiputra $course->lessons gives you a collection of items. $course->lessons->has(1) will check for index 1, not if the lesson has ID 1. If you have 6 items, the highest index will be 5, because array index starts at 0. Check the output of dd($course->lessons); or see if $course->lessons->has(0) returns true.

Snapey
Snapey
3 weeks ago (1,040,245 XP)

The correct function to use in this case is contains()

$course->lessons->contains('id',$lesson->id);
shez1983

@pramanadiputra uf the q doesnt provide useful info - how do you expect people to help you... ;)

pramanadiputra

@Nash

Thank you for your clear explanation, I didn't think about array and it index starts at 0 even though I knew it. Thank you

@Snapey Thank you, I'll try it

@shez1983 I think I've already provide useful information by explaining in the way I could, but if Snapey and you didn't find it with useful info, I'll try better next time I ask a question, with another form of sentence or another way of explaining it.

Thank You all, and Sorry.

Snapey
Snapey
3 weeks ago (1,040,245 XP)

Perhaps I could have stated it better. Sorry.

After the millions of uses of the framework, its you, not a bug

Your claim was that you had somehow discovered a bug in the framework. I was just exclaiming that its very unlikely that you have discovered a bug just lying there in the open, and that actually there is something wrong with your code.

pramanadiputra

@Snapey

Yeah sorry too, I took it personal so seriously, the way you stated sounds underestimate somehow.

But it also true that I haven't read Laravel Collection docs, and just had in mind that has() is the only function to do what I was intended.

Anyway, Thank You

Please sign in or create an account to participate in this conversation.