Skip to main content

Static, New Object and Laravel Auto-Resolving

Premium
5 min read

Have you ever wondered what happens behind the scenes in the Model when we call User::all()? This is, of course, only one example. In reality, you should understand the difference between such static calls and various other ways to create a new object. Let's explore the details.


Static Methods: Quick Shortcuts

As I mentioned, such a Class::method() call is a static way. Under the hood, it's this:

Illuminate/Database/Eloquent/Model.php

public static function all($columns = ['*'])
{
return static::query()->get(
is_array($columns) ? $columns : func_get_args()
);
}

As you can see, under the hood, it calls...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 44 min)

You also get:

56 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

K
katayamahide ✓ Link copied!

Hi, I think it should be doesn't care about a specific UserService object.

And this is totally fine. In this case, the store() method doesn't care about a specific User object - it just gets the array of data and saves it into the database. So, it's "stateless".

M
Modestas ✓ Link copied!

I'm not sure that this makes sense.

UserService works with User objects, not UserService objects

MR
Mark Ruys ✓ Link copied!

In the section 'Service Classes: Constructor with Parameters', public static function store(array $userData) should be public function store(array $userData) (non-static).

BM
Bogdan M ✓ Link copied!

literally, thought the same thing

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.