So for example if I have tables:
project
user_project
And I want to delete some project from project table and all record in user_project table related to that project, how can I do that?
Can I do that in single line? Because I don't want to some project be delted but in user_project to still exists...
If u set up delete cascading in your migration deleting a record in the project table will automatically delete the record from the related table if not then you have to manually delete it.
// to delete records from both table, let first delete from the
// child table then after that we delete from the parent table.
$project = Project::find($id);
// let assume you have define the user relation in your model and
// its a many to many association
$project->users()->detach();
$project->delete();
Related
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.
I am assigning multiple skills to an employee using checkboxes. when a user checks the checkbox a new record is inserted into pivot table. if a user unchecks the checkbox a record is deleted.
Now i am wondering if there is any function for insert or delete like there exists for new insert or update which is updateOrCreate to update an existing record or create a new record if none exists.
I can do it the hard way. but just want to know if there is any function for this like updateOrCreate.
Use the sync method it will replace the old values to new.
$employee->skill()->sync($request->checkedSkill)
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
Is it possible to delete automatically records from parent table when I delete connected records in child table in Oracle 10g. I found that I can remove child records when I delete connected parent records using ON DELETE CASCADE, but can I do reverse action?
Can the child table contain several child objects per parent? If not, simply define the delete constraint the other way around
I got two schema A and B. In A there is a table 'company' and in B there is a table 'newcompany'. Now I need all the data which is residing on A.company to be updated on B.newcompany whenever there is some data updation on A.company.
Please help me out with a query or some function which implements this functionality.
You could give grants to the user and simply access the table as B.newcompany inside an onupdate trigger for the A.company table.