When developing an application with Laravel, if it is relatively “large”, you might have to create model files that are similar, and that have no particular function: the primary key is “id”, they use the default timestamps, etc.
Since we are developing an ERP, I had to use dozens of templates, and I often found myself copying and pasting the same file, and just changing the name of the table, the rest being similar in all files. In addition, given that we do “customizable customization”, we have the core code of the ERP and the code specific to each project. This implied that I had to find a solution to create a “dynamic model” system. One solution that came to me fairly intuitively was to create a “generic” model, which can work with each “simple” model. And when the model required a particularity (like for example relationships), I just wanted to create the corresponding file, without having to modify everywhere in my code or this model was used,
The principle is therefore as follows: use of a helper function (as there are many with Laravel), which returns and instantiates: either the “specific” model if the file is created, or the generic model if the file doesn ‘ is not created. One of my constraints was that I then wanted to be able to use the standard of laravel with Eloquent.
Here are the steps to follow to use this type of configuration with Laravel:
1) Creation of a helper
So I created a file model_helper.php, which I declare in a provider to be able to use it.
The goal is to be able to use this helper in the following way:
$items = model('item')->orderBy('name')->get();
You understand that this code must work in an equivalent way, whether the App \ Models \ Item file is created or not.
The operation of the helper is therefore relatively simple: it will look for the “Item” file in App \ Models, and if it does not find it, it will then instantiate the generic model.
The code is relatively simple too, we will first find out which model we need to instantiate, to do this we will use the PHP standard “class_exists ()”, as follows:
$ models = array ( “\\ App \\ Models \\”. Ucfirst ($ model_name), “\\ App \\ Models \\ StandardModel”, ); foreach ($ models as $ model_class) { if (class_exists ($ model)) { $ model = new $ model_class (); break; } }
The operation of this code is very simple, it will check if a specific model class has been created, if not instantiate the standard and generic class.
Note: either it is not necessary to do class_exists () for the generic model, because we already know the answer, code to improve!
Note 2: in order not to lose performance, it is necessary to cache the result of the search for model class, so as not to systematically return to this loop.
2) Yes but then?
If we instantiate the specific model, nothing special to do, it’s exactly as if we did $ model = new \ App \ Models \ Item;
If we instantiate the generic standard model, it must be made to work with the Laravel standard, for this it is necessary to enter the necessary attributes, in particular one: the table attribute.
To do this, just do, following the helper:
$model->table = $model_name;
Of course, for that you must have a table name identified with $ model_name.
In addition, we add:
$model->primaryKey = 'id';
Another constraint to respect: your primary key columns must be “id” (if not, you can of course create a specific model if this is not the case!)
Then you can use your dynamically instantiated model like any Laravel model:
$item = model('item')->find(123);
$items = model('item')->where('...', '...')->get();
This allows in a few lines of code, to have a dynamic model system and very simple to use!
Ankit is detail-oriented professional with 12.5+ years of experience and a proven knowledge of programming, database development, and website architecture. Aiming to leverage his skills to the Full Stack Developer role at your company.
Comments are closed.