Laravel how to move data from table to another - laravel-5

Is there any simple way to move data from table to table using the id? I want to get all the info inside the first table using the id, insert it to another table, and then delete it from current one using Laravel.

Assuming both tables have the same schema you can do:
TableB::create(TableA::find($id));

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()

Dataset to sqlite data transfer

While development I was in need to create sqlite data base using dataset.
First create empty database file in sqlite. Then using dataset create table and dump data.
Is it possible using some dataset method?
I have done one thing
I have used one query
select * from sqlite_master where type='table'
That will give me schema of sqllite file from where I want to copy data.
On other end I will execute query return by above query.
That will help to create table dynamically.
On by one I used dataadapter to fill data and assigned dataset datatable data and used
Dataadapter. Update method.

Laravel Migration create table from existing table

I am working on such application where table audit should perform to make revision of updated records. Let's say for example I have to log each field update in Users update.
I wants to create clone of existing user table by
CREATE TABLE users_audit LIKE users;
My main question is,
Is there any alternative way or provision in Laravel migration to create table from existing table ?
I know we can run raw sql query in migration up/down method using \DB::raw();
But it would be helpful if any helper function available to create table from existing table like, create or table
Yes, you can, see the below link
Click this link
This statement is used to execute raw queries
DB::statement('CREATE TABLE tablename LIKE existingtablename');

updating data from one to table to another table which are in two different schema

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.

Can I create an Oracle view that automatically checks for new monthly tables?

I'm wondering if its possible to create a view that automatically checks if there is a new monthly created table and if there is include that one?
We have a new table created each month and each one ends with the number of the month, like
table for January: table_1
table for February: table_2
etc...
Is it possible to create a view that takes data from all those tables and also finds when there is a new one created?
No, a view's definition is static. You would have to replace the view each month with a new copy that included the new table; you could write a dynamic PL/SQL program to do this. Or you could create all the empty tables now and include them all in the view definition; if necessary you could postpone granting any INSERT access to the future tables until they become "live".
But really, this model is flawed - see Michael Pakhantsov's answer for a better alternative - or just have one simple table with a MONTH column.
Will be possible if you instead of creating new table each month will create new partition for existing table.
UPDATE:
If you have oracle SE without partitioning option you can create two tables: LiveTable and ArchiveTable. Then each month you need move rows from Live to ArchiveTable and clean live table. In this case you need create view just from two tables.
Another option is to create the tables in another schema with grants to the relevant user and create public synonyms to them.
As the monthly tables get created in the local schema, they'll "out-precedence" the public synonyms and the view will pick them up. It will still get invalidated and need recompiling, but the actual view text should need changing, which may be simpler from a code-control point of view.
You can write a procedure or function that looks at USER_TABLES or ALL_TABLES to determine if a table exists, generate dynamic sql, and return a ref cursor with the data. The same can be done with a pipelined function.

Resources