Populate table after running a migration - laravel

Background:
In my application, Users can own Files. It used to be that a File belongs to a single User, so I used a one-to-many relationship. Now, the requirements have changed, and my relationship needs to become many-to-many.
Previously, the data structure looked like this:
files
(
id,
user_id
...
);
and the new data structure looks like this:
files
(
id,
...
);
file_user
(
id,
file_id,
user_id,
);
Problem:
I created a migration to change my data structure, like this:
public function up()
{
Schema::create('file_user', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('file_id', 36);
$table->string('user_id', 36);
});
}
Then, I set up the relationship in the corresponding models. Now i need to convert the old data into the new data structure. That means taking all the existing Files, and creating a new record in the file_user table. The code itself is simple:
foreach(File:all() as $file) {
$file->users()->attach($file->user_id);
}
I want to run this code alongside the migration, so that after running the migration, the table will populate. However, when I try to put it directly in the migration file like this:
public function up()
{
// create table
Schema::create('file_user', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('file_id', 36);
$table->string('user_id', 36);
});
// convert old data
foreach(File:all() as $file) {
$file->users()->attach($file);
}
}
I get an exception:
PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "file_user" does not exist. It seems like the table has not been created yet at the time when the code is trying to run.
Question:
Is there a way that I can put the conversion code inside the migration file to make it work like expected? Or is there a better way to achieve this?

Try to create a second migration file, insert the // convert old data foreach(File::all() as $file) ... part there and migrate the two files together.

Related

Laravel - is there a library that lets me do a hasManyThrough relationship where my models PK is either value

Is there a library where I am able to use a pivot table that points to the same model twice (i.e User) where I can have the PK as either column 2 or 3 for something like a friending or associating system?
Code Example
migration
Schema::create('friends', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('requestor_id');
$table->unsignedBigInteger('requested_id');
$table->boolean('accepted')->default(0);
});
model
public function friends(){
return $this->[LibraryMethod](Friend::class, 'requestor_id', 'requested_id')->where('accepted', 1);
}
So then I could then either look at 'requestor_id' or 'requested_id' for my PK and get the PK of an associated User.
Ideally this should also work with eloquent functions.
I recognise that friends isn't the best example as you could have a handshake-like system, but for simple associations (i.e, between products) something like this would be pretty useful.

How to model a dependency graph in Laravel

I am stuck modelling the following relationship in a new application:
A Module can require (many) other Modules.
In Laravel I added the Module model and a ModuleRequirement model with its migrations. The second one has a foreign id field for the module it belongs to:
Schema::create('module_requirements', function (Blueprint $table) {
$table->id();
$table->foreignId('module_id')->constrained();
// How to reference other modules?
$table->integer('requires');
});
The relationship itself is defined by:
// Module.php
public function moduleRequirements()
{
return $this->hasMany(ModuleRequirement::class);
}
This method returns a collection of ModuleRequirements as expected. Unfortunately in order to get the real Module objects I need a foreach loop and build my own collection. I wonder if there is a simpler and faster solution. The perfect way I could imagine is sth. like $module->moduleRequirements returning a collection of the actual models instead of its ids.
My understanding that a module has relation with other modules in the same table.
you should use many to many relationship ,in that case you will not need a model for ModuleRequirement just a pivot table would be sufficient.
In your case module_requirements would act as the pivot table and you just need to change the relation in Module.php to :-
public function moduleRequirements(){
return $this->belongsTomany(Module::class,"module_requirements", 'module_id', 'requires');
}
the migration should be something like :-
Schema::create('module_requirements', function (Blueprint $table) {
$table->foreignId('module_id');
$table->foreignId('requires');
$table->foreign('module_id')->references('id')->on('models')->onDelete('cascade');
$table->foreign('requires')->references('id')->on('models')->onDelete('cascade');
});

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"):

Updating columns with migration Laravel 5.4

I have a running application, I see a field in a form is not required and can be left empty. but by the migrations, that field in database is set to be "not null". I wanted to to change it so I did it by checking Null. But how I do the same thing with migrations?? I read documentation and created this
public function up()
{
Scheme::table('modules', function (Blueprint $table) {
$table->text('content')->nullable();
});
}
but when I run migration, it give me error table already exists (obviously, because other migration files are there). How should I do it to achieve my target.
Change your code to this and notice the ->change() part:
public function up()
{
Scheme::table('modules', function (Blueprint $table) {
$table->text('content')->nullable()->change();
});
}
Documentation on changing columns
NB:
You'll need the Doctrine/DBal package in order for this to work:
Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column

How to create a new many to many record with attach

Okey, i seen some posts about this but i don't understand the concept of attach at all, i have three tables:
Llistes(Lists):
$table->increments('id');
$table->string('nom_llista');
$table->integer('user_id')->unsigned();
});
Cancons(Songs):
$table->increments('id');
$table->string('titol');
$table->integer('genere_id')->unsigned();
$table->integer('artista_id')->unsigned();
$table->integer('album_id')->unsigned();
Pivot table: llistes_cancons (lists_songs):
$table->increments('id');
$table->integer('id_canco')->unsigned();
$table->integer('id_llista')->unsigned();
$table->timestamps();
I have two other Classes that i think that are correct, but i''m not sure:
In Canco.php (Song.php):
public function llistescancons_llistes()
{
return $this->belongsToMany('App\Llista');
}
In Llista.php (List.php):
public function llistescancons_cancons()
{
return $this->belongsToMany('App\Canco');
}
So, the question is how can I implement in my controller a function that let me add new record to the pivot table (many to many) and if it's possible another funtion to show the records, i'm newbie in Laravel and it's a bit hard for me.
There's no need to implement methods to add/remove records from the pivot table. Eloquent has attach/detach methods that can do that for you, but first you need to give Eloquent the column names of the pivot table since you are not using Eloquent's column name convention.
In Canco.php (Song.php):
public function llistescancons_llistes()
{
return $this->belongsToMany('App\Llista','llistes_cancons','id_canco','id_llista');
}
In Llista.php (List.php):
public function llistescancons_cancons()
{
return $this->belongsToMany('App\Canco','llistes_cancons','id_llista','id_canco');
}
Then if you want to attach a song to list, you can easily use the song id to do that
$list = App\Llista::find(1);
$list->llistescancons_cancons()->attach($songId);
or the other way around
$song = App\Canco::find(1);
$song->llistescancons_llistes()->attach($listId);

Resources