I am interested in how to do MySQL column name suggestion.
Here is my simple database.
This is my php code in route/web.php
This is my about.blade.php file
I want it to suggest me MySQL column name like id, body, created_as, update_at.. like this $task->... and here to suggest me all the column names which has table.
Is this possible?
Sort of.
However, you wouldn't be able to use the DB class, you'd need to create a Model and create the appropriate #property tags in the docblock.
Sometimes it works, sometimes it doesn't I have noticed. It really depends if PHPStorm picks up the proper context. It likely won't be picked up automatically in blade templates but you can type hint any variable:
/** #var TaskModel $task */
There is a good plugin for IDE autocompletion with regards to Laravel here: https://github.com/barryvdh/laravel-ide-helper
Related
Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004
I'm trying to change an enum value in database with Laravel migrations.
In first, i have tried this classic change :
Schema::table('questionnaires', function ($table) {
$table->enum('type', ['image', 'sound', 'video'])->nullable()->default('image')->change();
});
But I got the following error :
Unknown database type enum requested,
Doctrine\DBAL\Platforms\MySQL57Platform may not support it
I solved my problem by doing SQL directly :
DB::statement("ALTER TABLE questionnaires MODIFY COLUMN type ENUM('image', 'sound', 'video') DEFAULT 'image'");
But it does not seem optimal to me...
Is there a solution more in "agreement" with Laravel 8,
without going through pure SQL?
Thanks
Explanation
To elaborate a little bit more on this subject.
As noted here ENUMs cannot be reverse engineered into a certain type. Each ENUM is a value of it's own. Hence, you must specifically say to Laravel which type the ENUMs are.
This seems to be an issue with all versions of Laravel
In documentation for each version, i.e. https://laravel.com/docs/8.x/migrations#renaming-columns, you can find that changing enum fields is not supported. As noted here the DB statement inside your migration is the best workaround for now.
DB::statement("ALTER TABLE questionnaires MODIFY COLUMN type ENUM('image', 'sound', 'video') DEFAULT 'image'");
Migration files fix
This option is the above commented GitHub link which advises you to put this line of code before the actual migration inside up() method of migration file.
DB::connection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Models/enum types fix
I think this one is a lot less straightforward then the above mentioned, but still exists as a valid option, so I will put it here,
You can read an official doctrine-project website page here which shows you how to this thing from within models / enum types.
I was looking at this post: Magento 2 Add a custom input field on chackout forrm and save it
I have tried the same way but I am not sure how this part came
$customField = $shippingAddressExtensionAttributes->getFiscalCode();
$shippingAddress->setFiscalCode($customField);
Where is this getFiscalCode?
It's a Custom AttributeCode.
the getFiscalCode() method is an accessor method that is used to identity fields such as the one that is created with the extension attribute fiscal_code mentioned in the example.
It is not just extension attributes, but all collection models in magento use the following camel casing structure where for example the column entity_id in database table, can be accessed via either $modelCollection->getEntityId() or $modelCollection->setEntityId($valueOfEntityId)
This is a good read to understand the idea Magento 2: CRUD Models for Database Access
i have an existing database and all my models are defined already. I would like to scaffold Controllers and Views because it's really time consuming.
i found this which sound good: infyom
They say in the documentation that you can use it from an existing database but any command keeps asking me for fields, (as it is trying to create a model from scratch), what i want to to use my existing models.
I found the "scaffold from table" option in the documentation:
php artisan infyom:scaffold Equipement --fromTable --tableName=Equipement
But it says:[ErrorException]
Undefined index: Equipement
i guess it did not find my existing model.
I was wondering, what do they call datatable anyway ? I can see in their config file they have a folder for that, maybe i should define my models there somehow ?
Thanks for anyone who can help on that. (or providing an alternative solution for scaffolding from existing database/models)
You must add id and:
updated_at` TIMESTAMP NULL DEFAULT NULL,
deleted_at` TIMESTAMP NULL DEFAULT NULL
in your table.
Im learning laravel.
My question is about some simple way do display model structure. I have little experience with django and as i remember, structure for each model was placed inside model files.
Yet in laravel, i need to put starting structure inside migration file:
$table->increments('id');
$table->timestamps();
$table->string('name')->default('');
Then if i want to add some new field, i will place this field in next migration file, etc.
So, is there any way to see some kind of summary for model? Maybe some bash command for tinker?
There are a bunch of options for you to choose from.
If you would like to show a summary of a model while you are in tinker, you can call toArray() on an instance of your model.
Ex:
$ php artisan tinker;
>>> $user = new App\User(['email' => 'john#doe.com', 'password' => 'password]);
>>> $user->toArray();
If you are trying to see a summary of a model displayed on your webpage, just var_dump or dd(...) an instance of your model after calling toArray() on it, and you'll get the same result as above, just in your web browser.
If you are looking for a way to show the table structure without creating any Model instances, you can display the table structure in your terminal, the exact command depending on what database you are using.
For example in MySQL you would do something like:
mysql> show COLUMNS from USERS;
It might also be a good idea to get a GUI app, I like Sequel Pro (for Mac).
P.S. I would just add that you should only have separate migrations for adding new fields when you are already in production and can't lose data from your database. While you are still in development and don't care about your data, it is much better to call php artisan migrate:rollback, add the new field to your create migration, and then php artisan migrate again, rather than making tons of new migration files.