panthro's avatar

Different where syntax?

I am aware you can perform a where via:

Post::where('slug', 'some-slug')->first();

But I have also seen:

Post::whereSlug('some-slug')->first();

There is no custom scope defined on the post model either.

Which one should be used, and why?

0 likes
4 replies
MohamedTammam's avatar

The are the same. whereSlug is a shorthand for where('slug', 'some-slug'). whereSlug is using where under the hood.

1 like
Snapey's avatar

Its magic developer convenience.

You can do this for any column in the table. It makes zero difference to runtime performance. At best it makes a more 'eloquent' looking code, at worst confuses people new to the project, looking in the docs trying to find a whereSlug method

1 like

Please or to participate in this conversation.