Laravel Schema Builder, setting field description - laravel

Is it possible to add a description/comment to the sql field with laravel's schema builder. Just like in drupal?

It turns out that you can add comments, but it doesn't seem to be documented. This Laracasts post shows how – by adding a "comment" property to the end of the line.
Using their example,
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->string('product_name')->comment = "Product name column";
$table->timestamps();
});
}
As it turns out – just testing now – you can actually use the more typical function syntax for that, so for example,
$table->string('product_name')->comment('Product name column');
...similar to setting ->default(...) or ->nullable(). Some people might prefer that style for consistency.
This seems to work great as of Laravel 5 and using MySQL. It might be a recent improvement.

Descriptions / comments are not supported by the schema builder and probably won't in the future. You have to fall back to SQL:
Assuming you use MySQL
Schema::create('users', function(Blueprint $table){
$table->increments();
$table->text('username');
$table->text('password', 60);
});
DB::statement('ALTER TABLE `users` CHANGE `password` `password` VARCHAR(60) COMMENT 'password hash');

Related

Change the datatype in the column with data in laravel migration

This is the migration? i have to change the string data column into integer data column with existing data
public function up()
{
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->integer('tena_type')->unsigned()->nullable()->change();
$table->foreign('tena_type')->references('id')->on('account_types');
});
}
As per laravel Documentation you can create a new migration and do it like this:
Schema::table('SYS_tenants', function (Blueprint $table) {
$table->integer('tena_type')->unsigned()->nullable()->change();
});
Before modifying a column, be sure to add the doctrine/dbal dependency
to your composer.json file.
composer require doctrine/dbal
Reference: Laravel -> Database: Migrations-> Modifying Columns
You can use change method on the field which you want to change the type after setting the new field type.
public function up() {
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->string('tena_type')->change();
});
}
I supposed the migration which create the table has already call all requirement you need like unique, nullable and so on. You can call change method, by the way there isn't restriction about any modification you want to perform like add other mysql index on that field.
Do not forget to add doctrine/dbal in composer.json file
Migrations#Modifying Columns
Looks like what you have should work:
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->integer('tena_type')->unsigned()->nullable()->change();
});
Depending on your database you may need to cast the values to the new type: (for mysql: https://www.mysqltutorial.org/mysql-cast/)
I already use this Laravel Migration
$table->integer('tena_type')->unsigned()->nullable()->change();
But it doesn't work because, the data already in the table. In that case it can't change the datatype.I use this DB statement to change the datatype with data.it's working properly.
DB::statement("alter table SYS_tenants modify tena_type integer not null"):

Error with index when converting Laravel mysql migrations to postgresql

I have written about 10 Laravel migrations for a MySQL database. I want to now switch out my database for a Postgresql database but am having some trouble with indexes it seems.
I followed a tutorial about a voting module so I didnt write the migrations myself but they did all work when migrating on MySQL.
The error im getting is as follows;
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "poll_id" already exists (SQL: create index "poll_id" on "gp_poll_votes" ("poll_id"))
There is no duplicate table and these migrations have been working for the last year or so.
The migration it is getting stuck on is as follows;
Schema::create('gp_poll_votes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('poll_id')->unsigned();
$table->bigInteger('poll_option_id')->unsigned();
$table->bigInteger('vote_count');
$table->timestamps();
$table->index('poll_id', 'poll_id');
$table->index('poll_option_id', 'poll_option_id');
$table->foreign('poll_id')->references('id')->on('gp_polls')->onDelete('cascade');
$table->foreign('poll_option_id')->references('id')->on('gp_poll_options')->onDelete('cascade');
});
There is 2 more migrations associated with the votes which are run before the erroring one, which are;
Schema::create('gp_polls', function (Blueprint $table) {
$table->bigIncrements('id');
$table->tinyInteger('status')->nullable(false)->default(1);
$table->timestamps();
});
and
Schema::create('gp_poll_options', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('poll_id')->unsigned();
$table->bigInteger('image_id')->unsigned();
$table->tinyInteger('status')->nullable(false)->default(1);
$table->timestamps();
$table->index('poll_id', 'poll_id');
$table->index('image_id', 'image_id');
$table->foreign('image_id')->references('id')->on('gp_submit_images');
$table->foreign('poll_id')->references('id')->on('gp_polls')->onDelete('cascade');
});
In Postgres the index names have to be unique across the database it would seem, in MySQL that doesn't seem to matter.
You can create your indexes without passing a second argument, the name of the index, and Blueprint will create a more unique index name for you.
$table->index('poll_id'); // "gp_poll_options_poll_id_index"
It will use something like "{$prefix}{$table}_{$column}_{$typeOfIndex}" to generate the index name for you.

How to get relation 'table1' OR 'table2' OR 'table3' into one table in laravel?

I'm a newbie here and also in Laravel, so please excuse me. I have a table named 'products' and this table related to the 'recipes' table via many-to-one relation(One of the recipes has a lot of products). -'recipes' table keeps reference code- Here's where I stuck; the 'recipes' table has one-to-one relations to three different tables that keeping the "real" product recipes. Those tables have different recipe contents like,
Alkaline table;
Schema::create('alkalines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_bicarbonate');
$table->timestamps();
});
Acets table;
Schema::create('acets', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_chloride');
$table->integer('acetic_acid');
$table->timestamps();
});
I'm able to fetch all relations if I start with one of these(e.g with Acet model). But if, I list all of products and try to fetch it's recipe, I have to use a bunch of 'if and else's. Just can't get the recipe like;
$product->recipe-> "one of the three recipe tables' content"
And my 'recipes' table:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ref');
$table->timestamps();
});
I believe it's easy, just missing something. Please help! Thanks in advance!
I think
You can get every relation individual them merge the arrays
like
$arr1=Alkalines::with('recipe')->get()->toArray();
$arr2==Acets::with('recipe')->get()->toArray();
$arr3=***************************;
array_merge($arr1,$arr2,$arr3)
Welcome to SO.
If you have relations set up properly, you can use 'collection->pluck()' to retrieve their results, no matter how deeply nested in different relations.
Example:
$game->players->stats won't work, because players is a collection that doesn't have a stats attribute, method or field.
So, what you can do is use pluck() and collapse() to retrieve result of relations:
$game->players->pluck('stats')->collapse()
I edit the code from #mohamedhassan a little bit, and it worked!
public function solutionMerge()
{
$arr1=Alkaline::with('recipe')->get();
$arr2=Acet::with('recipe')->get();
$arr3=Glucose::with('recipe')->get();
$solutionMerge = collect([$arr1,$arr2,$arr3]);
return $solutionMerge;
}
Just assigned arrays into a collection. And then use collapse().
And now, I'm able to fetch data like $solutionMerge->recipe->id
Thank you people, for your precious time and immense knowledge!

Correct approach to making down migration for making column nullable type

I have a migration defined like so:
Schema::table('campaigns', function (Blueprint $table) {
$table->string('rates')->nullable(true)->change();
});
So I'm making "rates" nullable.
How should I handle the reverse migration? If there's no data in that column do I just set a default? I can't make it not null because that would violate integrity constraint. Can I not do anything about it?

Set default to NULL with laravel migration

I am adding a field to a table in a migration that I wish to allow to be NULL but also I wish for it to default to NULL. What do I place in the default method? I fear that putting "NULL" in will attempt to place a string of NULLin which I obviously don't want. Please help :)
Schema::table('item_categories', function(Blueprint $table)
{
$table->integer('parent_item_category_id')->unsigned()->nullable()->default($what_to_put here);
});
When you use the nullable() method on a field, that field will default to NULL.
$table->integer('parent_item_category_id')->nullable();
To make the column "nullable", you may use the nullable method:
$table->string('email')->nullable();

Resources