How to return id of a record I just entered using Laravel?

Asked

Viewed 243 times

-3

I’ve been messing with Laravel for a little while and I’ve got a problem... I need to return the id of a table in the database to feed the foreign key from another table and I’m not getting it.

How did I get the id that was entered last?

    $customer                 = new Customer();
    $customer->name_customer  = $request->input('name');
    $customer->cpf_customer   = $request->input('cpf');
    $customer->email_customer = $request->input('email');
    
    $result = $customer->save();
    
    $sales                    = new Sale();
    $sales->date_sale         = $formatDate;
    $sales->quant_sale        = $request->input('quantity');
    $sales->deduction_sale    = $finalValue;
    $sales->name_product_sold = $request->input('product');
    $sales->status_sale       = $request->input('status');
    $sales->sales_customer_id = $customer->customer_id;
 
    $result = $sales->save();

I created the object Customer and I’m trying to catch the customer_id down, but it’s coming like null

Laravel Framework 7.25.0 Mysql

  • What version of Laravel and PHP are you using? What database? This information is important for us to help you. If possible include this information in your question.

  • If my answer answered what you wanted, please mark as the answer to your question to be answered.

2 answers

0

If your table is structured with the Laravel standard (using "id") it would look like this:

$customer                 = new Customer();
$customer->name_customer  = $request->input('name');
$customer->cpf_customer   = $request->input('cpf');
$customer->email_customer = $request->input('email');

$result = $customer->save();

$sales                    = new Sale();
$sales->date_sale         = $formatDate;
$sales->quant_sale        = $request->input('quantity');
$sales->deduction_sale    = $finalValue;
$sales->name_product_sold = $request->input('product');
$sales->status_sale       = $request->input('status');
$sales->sales_customer_id = $result->id;

$result = $sales->save();

If you are made a relationship following also the pattern can be so too

$customer                 = new Customer();
$customer->name_customer  = $request->input('name');
$customer->cpf_customer   = $request->input('cpf');
$customer->email_customer = $request->input('email');
$customer->save();

$sales                    = new Sale();
$sales->date_sale         = $formatDate;
$sales->quant_sale        = $request->input('quantity');
$sales->deduction_sale    = $finalValue;
$sales->name_product_sold = $request->input('product');
$sales->status_sale       = $request->input('status');
$customer->sales()->save($sales);

This is in case your Costumer model has a relationship called Sales() you can do so straight as you already get the foreign key automatically.

Remembering that I talked about the "Laravel patterns" but there is nothing wrong with using your own pattern.

https://laravel.com/docs/7.x/eloquent-relationships#the-save-method

-1

After saving, $data->id must be the last id inserted.

$data->save();
$data->id;

It can also be used in your case:

$sales->sales_customer_id = $customer->id;

But I take very carefully your id needs to be timo AUTO-INCREMENTED otherwise it can be an error if it is of the string type.

Also you can’t forget to set the variable below in the MODEL as true or it will return as empty.

public $incrementing = true;

For example:

    <?php
    
    class Voar extends Model
    {
        /**
         * Indicando se o ID é auto-incrementado.
         *
         * @var bool
         */
        public $incrementing = true;
    }

link to the official Laravel documentation here: https://laravel.com/docs/7.x/eloquent

  • forgive friend but did not understand, after taking the foreign key of the table 'Buy' I need to pass as parameter to feed the table 'sale'...in this case I would pass the "Answer()->json"?

  • Using the code $sales->sales_customer_id = $customer->id; , but you need to include the parameter in your class declaration public $incrementing = true;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.