Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of utility functions in Laravel

Tags:

php

laravel

Good day to all, I am new to Laravel and the thing that I encountered with and cannot understand is utility methods that are used in the framework, for example, Customer::all() where Customer is model or Customer::find(). So, what is the point of utility methods and why all() and find() are static.

like image 722
Mirasan Avatar asked Mar 06 '23 12:03

Mirasan


1 Answers

Utility functions in general offer some sort of utility, i.e. functionality which is usually just for convenience and can also be achieved by following a set of steps.

For example Model::find() is functionality that can also be achieved by creating a query builder object and then performing a query e.g.

Model::getQuery()->where('id', '=', $id)->first();

For convenience you can just do Model::find($id) which is shorter and more intuitive.

It is static because it does not make sense for it not to be static. A non-static method requires an instance of the class however in ORMs an instance of a class corresponds to a potential database entry (which either exists or could exist). Therefore since find is used to retrieve a database entry it makes no sense to require a backing database entry in order to use it.

In short what this means is, if you execute method $object = Model::find(1) you will get back a model which corresponds to database entry with identifier 1. There is a 1 to 1 mapping of the PHP object to the relational object. If make changes to $object and call $object->save() then changes will be persisted in the database (i.e. $object already exists). If on the other hand you do $object = new Model() you will get back a new model which does not currently correspond to a database entry. However calling $object->save() will make that object correspond to a new database entry and it will be updated accordingly (i.e. the new object could exist). If a framework required you to make a "dummy" object just to access some helper methods there's always a chance that (either by omission or through unknown side-effects) save() gets called on that dummy object and you end up filling up your database with what is essentially junk.

The takeaway from this is that in an ORM it does not make sense to create a model instance if you don't intend to store it in the database. Laravel does not strictly obey this rule, but in general it does, and you should too.

The same applies to all which gets all database entries. It doesn't make sense to get all database entries by requiring to first get one entry.

Same applies to getQuery used above which returns a query builder instance. Note that most people don't actually use getQuery because it runs implicitly when someone uses something like e.g. Model::where(..) so technically Model::where() is also a utility. I mention this because when you see something like Model::where('id', $id)->where('name', $name) the 2nd where is after a -> which implies it's on an instance rather than static, however that instance is actually a query builder instance and not a model instance.

like image 187
apokryfos Avatar answered Mar 08 '23 21:03

apokryfos