It seems to me that having duplicate createTable() methods in both a migration and a model is a bad/dangerous thing. I would worry that someone will change the model and forget to change the corresponding migration (or vice versa), causing confusion and errors.
Does anyone know if it is possible (or wise) to simply call the createTable() method in the model from within the migration script?
If so, can someone point me to a working code sample so I can see how it is done?
I'm going to mark this as answered, per the comments above. It appears that the createTable() method does not belong in the model. I was fooled by a "more experienced" Laravel developer who did it that way.
:-/
Related
I am new at Laravel.
As I am studying Laravel I noticed that ALL Model I made is named by the plural but it works properly.
I am wondering how can it happen because I read documents after I noticed that and it says like, "Model should be named by the singular.".
Can anyone explain how it works??
P.S. There is no mention $table in those Model.
I am so sorry I wanted to say "singular" not "syllable".
General convention of laravel is using a singular name for a model and a plural name for a table. Note that you can change the associated table name by using protected $table = 'yourTableName'; in your model. I personally think setting it manually is a good practice.
I can't explain how it gets the associated table name as it will be an essay(also I am not sure I will be able to explain properly) but I can send you to the right direction.
As laravel is open source you can actually peak under the hood and see how it works. You can go to framework/src/Illuminate/Database/Eloquent/Model.php or see it online to see the associate functions. You will see the getTable function which will return the table name. But it uses another function. So you need to see what is in that function.
After a few digging you will see that laravel actually uses a different library to help getting the project name. It uses inflector. You can browse a bit about it to understand how it gets the plural name. There you can find the underlying code and rules etc to understand how it works.
Hello guys, I created a "promos" table and now, I am working on the CRUD functionalities of this module. The "create" functionality is done and I encountered no problems. My only problem is this when I am updating my model and it is very weird.
It seems that $this->model->where('id', $id)->first() cannot see and retrieve the list of columns. Here is the screenshot.
I already tried composer dump-autoload and php artisan clear-compiled hoping the problem will fix itself.
For additional reference, here is my schema, model and code:
Other notes: $this->model points to the Promo model
EDIT:
I did not display the controllers specifically the update method as stated by #OmarTarek. Our company is using RepositoryInterface Pattern. Instead of the normal View=>Controller=>Model when saving data to the database, our workflow is like this View=>Controller=>Repository=>Model
In my controller, my code is
While in my repository, my code is like this:
As you can see, I am inheriting the BaseRepository.php because it has all the necessary functions/methods for create, update and delete.
It is the BaseRepository
I highlighted the code that is giving the error.
EDIT II:
I already implemented the change suggested by #PaladiN. The error still displays and the update method still don't work.
You can remove the first() since the id is a primary key. Also you're calling the update statement again on updateData() method, you should remove that.
$this->model->where($key, $value)->update($data);
Also when you either define $fillable or $guarded, not both. When you define $guarded with an empty array, all the fields become fillable by default. Another thing would be to check if the model created in the constructor using make() is a valid model instance before proceeding.
sorry for the late update. I just fixed this error but I still don't know how this error happened. It seems that a mutator from our BaseModel.php interferes with the updating of data.
I tried overriding the mutator in my Promo.php model to make the update method work and the error no longer shows.
I still don't know what caused the error but I am eager to deliver this module first to our clients so I will investigate how this scenario happened next time.
Thanks guys.
My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.
I sometimes see the function parentNode() in cakePHP models. What is the purpose of that function and when should I use it? If I don't use it, what problems am I going to encounter.
On this website, they say that you need it so that your model can have ACL behaviors, but what if I just run the build_acl command after creating my Model, isn't that the same thing? Can someone shed some light on this please?
Thank you
build_acl() is useful as a one-time execution to populate your acl tables with the current controllers+actions. This is useful as a base to get you started.
parentNode() is called by the model behaviour in afterSave() to maintain the hiearchy during the life of your application. So when you manually (or dynamically) add AROs / ACOs later on (e.g. 5 months from now) everything will still work. Especially true if you add ACOs with custom aliases.
BTW, best ACL tutorial I've found:
http://net.tutsplus.com/tutorials/php/how-to-use-cakephps-access-control-lists/
In my models I'm setting them up using constructors like the following
function Areas()
{
parent::Model();
$this->db->from("areas");
}
However, if a method of my model queries the database several times it looks like the FROM clause is only included in the first query. Is there an easy way to persist the FROM for all queries run within my model (unless I manually override it)?
I would advise against this. It would lead to REALLY hard to smush bugs later on down the line. The problem, I'm guessing, has more to do with the name of the table changing? In which case, you can use a constant for each table and change the name in config.
If you just wanted to save a line of code, sometimes more lines is better. This is the case here.