Seeding a Pivot Table in Laravel 5 - laravel-5

How should I effectively seed a pivot table regarding the fact that when in the seeder class PostTagTableSeeder it asks to set the path of the table's model
TestDummy::times(10)->create('App\Model');
but as pivot tables don't have models I dont know what to fill.
should I use the table's name? like
TestDummy::times(10)->create('post_tag');

Related

Join database table based on Json colum having multiple key

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.
You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

Laravel Model - should I cast foreign index integer keys?

When I make a new table in Laravel, I generally use the Laravel default id column definition: $table->bigIncrements('id'); Say for example I have a clients table with that in my migration.
If I need a foreign relationship on another table, I 'll do it like this:
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
However, if I get model instances of both of those tables and render them with ->toJson();, the id of the first table will be an integer in JSON, while the client_id of the second table will be a string in the json output.
Now I can make a $casts=[] array in the Model class for the second table to cast it to an integer, but I don't know if there's any reason I shouldn't do that. Or maybe I should ALWAYS do that. Is there any guidelines or suggestions about this sort of thing?

Laravel data migration to new structure

We are planning a major database restructure and instead of "Create" all the migrations are "Alter"-ing already existing tables.
For example:
There's table users that holds a column, e.g. role.
After running migrations this column will be dropped and table "Roles" will be created with a hasOne relationship (just an example).
How should I approach converting the old data into the new structure as seamlessly as possible?
It depends on the environment you are working in. for example in production level you can't just move those values from one column to another because the database Is heavily under usage of customers. but for instance you can create a migration to only create the roles table. then create a new migration to copy the corresponding role of each row to the newly created table(roles).
Roles
user_id | role_id
Users
id | role_id
After moving the whole role_id from users table to Roles table then you can push the new code implementation.
I don't really like the idea of removing the role_id column from users table immediately. i would suggest to keep that column for a while till you finish the whole changes related to that column then you can remove it safety without worry about it.

How to delete an entry in a table along with its data in another table?

I have a table cars with attributes: id, name. I also have another table specs with id, car_id, name. This tables are related with one to many relations from the Models. I also have set up the foreign keys in.
I have a controller manageData where i have a function insertCar which i use to insert data and update both tables. I wan to create another function deleteCar from where i can delete the car along with its specs from the other table
Use onDelete() method in migration when defining foreign key:
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
In this case when you'll delete a car record, related data will be automatically deleted from another table.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints

Link tables with extra column in Symfony 2 / Doctrine

In a database I have the tables User, Group and UsersInGroup and generate the entities from the database using the Symfony 2 console. The table UsersInGroup is not generated as an entity but is partly generated into User and Group. This would be perfect if UsersInGroup hat only two columns userId and groupId (together primarykey), in this case the table UsersInGroup also contains a third column Role. This field is can never be filled using the generated entities
How should I fill the Role column in the tabel UsersInGroup?
You have to declare your relation table manually and to map the relations.
You'll find an interesting discussion here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table

Resources