How to insert data into two tables in mysql - Laravel -
i want insert data 2 tables of mysql,
tables follows:
1) t_entity_details 2) t_preferences
columns inside table 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 submit form, entity_id should same in both tables. how that? please me this.
the solution simple. can use "mysql_insert_id" last incremented/inserted id. can use in turn insert second table.
in eloquent, things more simpler, assuming know eloquent models,. insert 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 last insert id follows:
$entity_id = $saveresult->id;
you use insert second table:
$insert_secondtable_array = array("foo" => "bar"); $insert_secondtable_array['entityid'] = $entity_id; $result = secondeloquentmodel::create($insert_secondtable_array);
this inserts both tables.
Comments
Post a Comment