When registering to the auto_increment id column of table A, I want to register the same column name id in another table at the same time
Please write your question in detail here.
We practice DB operation by learning PHP (Laravel).
There is a column called user_id of auto_increment in table A, and I want to store the same number in the column with the same name as user_id in table B when I put data there.
I found that I can get id by using insertGetId when inserting data into table A, but I'm still troubled by checking how I can use it to reflect in table B.
// Describe the following in the TableA model. Get the id with this (notation method that I searched Gugu)
$id = \ DB :: table ('table_A')->insertGetId ($data, 'user_id');
// TableB model is usually only insert
\ DB :: table ('table_B')->insert ($data);
// Unknown how to write to controller.
public function create (Request $request) {
$data ['user_id'] = $request->user_id;
$data_id = \ App \ Models \ tableA :: insert ($data);
}
Changed from the above to the following with reference to the comments
Added the following to Model A
protected $primaryKey = 'user_id';
----------------------------------
Changed Controller to
$a = new \ App \ Models \ A ();
$a->save ();
$b = new \ App \ Models \ B ();
$b->user_id = $a->user_id;// $a's user_id is auto_increment
$b->save ();
The following error occurs after correction
Error description
SQLSTATE [23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ~
I think there are a lot of deficiencies, but if you know a wide variety of notation, please professor.
Supplemental information (FW/tool version etc.)OS: Mac (catalina10.15.1)
PHP: ver7.3
Laravel: ver5.8
Mysql: ver5.7
-
Answer # 1
-
Answer # 2
Is it possible to process with trigger?
Not only when inserting but also what you want to do when deleting or updating (changing to another number)
I think it ’s better to consider
Related articles
- php - laravel 55 membership registration page is not displayed
- php - how to get user name from laravel relation offer table
- php - change data in laravel intermediate table
- php - how to get when model of laravel is view instead of table
- how to create a new table with the user name as the table name [php + mysql]
- php - i want to register an image on the laravel admin management screen
- php - laravel multi-login feature doesn't work
- php - how to get the values of a joined table?
- php - unexpected data found in laravel todo app tutorial data missing
- php - laravel folder creation function hypertextcandy tutorial chapter 8
- php - laravel relation cannot be defined
- php - i want to prevent escaping when outputting {{}} with laravel echo
- php - [laravel 6] regular expression validation "|" handling
- php - laravel symlinks don't work
- php - the controller specified in laravel apiresource cannot be found
- php - laravel controller
- php - laravel parent view file function receiving and routing
- php - [laravel] command execution using guzzle is not possible
- php - i can't access the value i got using get in laravel from view
- php - laravel page can no longer be moved
- php : Laravel -validation on multiple input file
- i want to display the selected radio button on the laravel php edit screen
- php - [laravel] how to read and use the class of another file in controller
- php - about the value stored in laravel db
- php - e-mail transmission with laravel is garbled
- after upgrading the php version of the rental server from 58 to 74, composer stopped working on ssh connections
- php - i can't connect to mysql in laravel js
- php - i built an environment with docker and laravel, but i get a 404 file not found
- php - about searching with laravel where clause
- php - left join doesn't do what i want
For Laravel, use Eloquent (model)
The id when saved is automatically set to the id of the instance.
So if you write simply, it is the following procedure.
If you really want to use the column name user_id instead of id
Set $primaryKey property to model A and make any column the primary key