S
S
suhuxa12017-10-18 13:35:05
Laravel
suhuxa1, 2017-10-18 13:35:05

Is it possible to get the id of a newly added post in laravel?

The algorithm is as follows: I receive and process a whole array of information. Some of this information is stored in the main table, for example, car. Other part of the information should be placed in other tables, for example, color, price. These id tables are linked. Those. in the car table there is the id of this very car, then it is added to the color table - the color of the car and the id of the car. In the price table, respectively, car ID and price. So, how to add a piece of information to car and get the id of the added record, so that using this id to add another piece of information to the rest of the tables?
Thanks to!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-10-18
@suhuxa1

$id = Example::insertGetId([
    'key1' => 'value1',
    'key2' => 'value2'
    // ...
]);

Or
$id = Example::create([
    'key1' => 'value1',
    'key2' => 'value2'
    // ...
])->id;

Where Example is the Active record model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question