insert data as an array in MySql using laravel 5.4 - laravel-5.4

How i can insert multiple id of a table in a single field of another table using laravel 5.4 for exemple :
--------------
field
--------------
2-6-12-7...
--------------

At first, you can make multiple separated ids as a string and then you can insert. Before inserting you have to sure that the field type is varchar or string.
Code Example
$userIds = User::select('id')->get()->toArray();
$joinIds = implode("-", array_column($userIds, "id"));
Now I will Insert into another table which name is Post and has a column name is user_id. The column user_id type will be varchar or string.
$post = new Post();
$post->user_id = $joinIds;
$post->save();

Related

Replace string in column value MYSQL

Hi i have a table named data_table with columns ID | DATA
Id is integer and Data stored like this:
a:19:{s:10:"store_name";s:9:"STORENAME";s:6:"social";a:7:{s:2:"fb";s:0:"";s:7:"twitter";s:0:"";s:9:"pinterest";s:0:"";s:8:"linkedin";s:0:"";s:7:"youtube";s:0:"";s:9:"instagram";s:0:"";s:6:"flickr";s:0:"";}s:7:"payment";a:2:{s:6:"paypal";a:1:{i:0;s:5:"email";}s:4:"bank";a:0:{}}s:5:"phone";s:0:"";s:10:"show_email";s:2:"no";s:7:"address";a:6:{s:8:"street_1";s:0:"";s:8:"street_2";s:0:"";s:4:"city";s:0:"";s:3:"zip";s:0:"";s:7:"country";s:0:"";s:5:"state";s:0:"";}s:8:"location";s:0:"";s:6:"banner";i:0;s:4:"icon";i:0;s:8:"gravatar";i:0;s:14:"show_more_ptab";s:3:"yes";s:9:"store_ppp";i:12;s:10:"enable_tnc";s:3:"off";s:9:"store_tnc";s:0:"";s:23:"show_min_order_discount";s:2:"no";s:9:"store_seo";a:0:{}s:24:"dokan_store_time_enabled";s:2:"no";s:23:"dokan_store_open_notice";s:0:"";s:24:"dokan_store_close_notice";s:0:"";}
Also i have another table named user_stores ID | STORE
Id is integer and store is string format.
I want to make trigger on update when table user_stores change some store name then change the a s:9:"STORENAME
s:9 is the length of the value, in our example is STORENAME
Assuming you're using MySQL, you can create a trigger on the user_stores table to update the data_table when a store name changes. Here's an example:
CREATE TRIGGER update_storename_trigger
AFTER UPDATE ON user_stores
FOR EACH ROW
BEGIN
UPDATE data_table
SET DATA = REPLACE(DATA, CONCAT('s:', LENGTH(OLD.STORE), ':"', OLD.STORE, '"'), CONCAT('s:', LENGTH(NEW.STORE), ':"', NEW.STORE, '"'))
WHERE DATA LIKE CONCAT('%s:', LENGTH(OLD.STORE), ':"', OLD.STORE, '";%');
END;
This trigger will be executed after the user_stores table is updated and will update the DATA column of the data_table if the store name has changed. The REPLACE function is used to replace the old store name with the new store name in the serialized data stored in the DATA column. The WHERE clause is used to search the data table rows that contain the previous store name in the serialized data.
Note that this trigger assumes that the serialized data in the DATA column is in the serialized format. If the data is in a different format, the trigger will have to be modified.
The final answer who works is:
BEGIN
UPDATE data_table as b
SET b.DATA = CONCAT(SUBSTRING_INDEX(b.STORE, 'store_name', 1),'store_name\";s:',LENGTH(new.STORE),':\"',new.STORE,'\";s',SUBSTRING_INDEX(b.STORE, SUBSTRING_INDEX((SUBSTRING_INDEX(b.STORE, 'store_name', -1)), ':',3), -1))
WHERE b.ID=new.ID
END;

lastInsertId(tableName_id_seq) returning :Undefined table: 7 ERROR: relation "tableName_id_seq" does not exist

inserting data in my table in a foreach, and i need to have the last ID inserted.
Im inserting a first entity, get the Id and insert a child entity, linked with this ID.
Database POSTGRES - ENV Symfony 4
Code :
$conn = $em->getConnection();
//Here i do an Insert ( always an INSERT )
$Id = $conn->lastInsertId('tableName_id_seq');
This return :
Undefined table: 7 ERROR: relation "tableName_id_seq" does not exist
I feel like my sequence are not created in the DB
I do not close connexion before calling lastInsertID
It's Always un insert before lastInsertID
This Query:
select sequence_schema, sequence_name from information_schema.sequences;
Return 0 rows
With MySQL you don't need to provide the tableName_id_seq
Can you just try $Id = $conn->lastInsertId();
Well no way using lastinsertId(),
I found my way using
RETURNING id
in the Query, and then after the
$preparedQuery->execute()
i do a
$preparedQuery->fetchAll()

Copy table data on same server with field remapping

I need to copy the data of an old table with millions of rows to a newer table, with a slightly different definition. Most importantly, there is one new field with a null-default, and a varchar field became an enum (with directly mapping values).
Old table:
id : integer
type : varchar
New table:
id : integer
type : enum
number : integer, default null
All of the possible string values of type are within the new enumeration.
I tried the following:
insert into new.table select * from old.table
But I obviously get:
Insert value list does not match column list: 1136 Column count doesn't match value count at row 1
You can copy the table data and structure from phpmyadmin window, and then modify the new table and add the new column.
Using the INSERT ... SELECT syntax:
INSERT INTO new.table `id`, `type` SELECT `id`, `type` FROM old.table
Apparently the varchar to enum remapping isn't a problem.

How to insert data into two tables in mysql - Laravel

I want to insert data into two tables of mysql,
Tables are as follows:
1) t_entity_details
2) t_preferences
Columns inside the table are as follows:
t_entity_details
- Entity_id (primary key, auto-increment)
- First_Name
- Sex
- TagLine
- Personal_Desc
- DOB
- Lang
t_preferences
- Entity_id (primary key too, No auto-increment)
- other columns......
Now when I submit a form, the Entity_id should be same in both the tables. So how to do that?
Please help me with this.
The Solution is simple. You can use "mysql_insert_id" to get the last incremented/inserted id. You can use that in turn to insert into your second table.
In Eloquent, things are even more simpler, Assuming you know about Eloquent Models,. You insert to the first table:
$insertArray = array('First_Name' => 'Some_value',
'Sex' => 'Some_value',
'TagLine' => 'Some_value',
'Personal_Desc' => 'Some_value',
'DOB' => 'Some_value',
'Lang' => 'Some_value');
$saveResult = EloquentModel::create($insertArray);
You can then get the last insert id as follows:
$entity_id = $saveResult->id;
You use this to insert into the second table:
$insert_secondTable_array = array("Foo" => "bar");
$insert_secondTable_array['EntityId'] = $entity_id;
$result = SecondEloquentModel::create($insert_secondTable_array);
This inserts into both tables.
In this scenario do the query statement as,
insert into t_entity_details (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
insert into t_preferences SELECT * FROM t_entity_details;
i believe this type will solve your problem if you don't do manipulation using other columns.

Trigger on MySQL tables with variable?

I need to create in MySQL trigger on such schema:
mail
oid id
varchar name
varchar reference
adress_mail
oid id
oid id_mail
oid id_adress
adress
oid id
varchar name
after something is inserted into adress_mail it is updating 'mail.reference' with value like every 'adress.name' for example:
I am inserting adress_mail with (1,5,6) and (1,5,7) so trigger will update 'mail.reference' with values from 'adress.name where adress.id = 6' and 'adress.name where adress.id = 7'
Q: The whole problem is how to loop on adress_mail table and get all
'adress.name' in 1 varchar and then just update 'mail.reference' with
that value?
I can setup something like var (variable) in trigger and then collect all results from select?
PS 'mail.reference' it's varchar row to improve some searching feature of application, because I don't have possibility to loop on 'adress_mail' table.
Just don't do that ! You are using an sql data base, and the main idea of it is table relations, so when you'll have to look for data from address linked to mail, perform a select with joins on address_mail.