1

I am beginner in Laravel. I use in my project Laravel 5.8.

I have this code:

$product = new Product();
$product->create([
    'company_id' => $request->user()->company_id,
    'enable' => $request->input('enable') ?? 0,
    'id_delivery_vat' => $request->input('id_delivery_vat') ?? 1,
    'id_product_vat' => $request->input('id_product_vat') ?? 1,
    'id_producer' => $request->input('id_producer'),
    'promo' => $request->input('promo') ?? 0,
    'name' => $request->input('name'),
    'qr_code' => $request->input('qr_code'),
    'oe_code' => $request->input('oe_code'),
    'description' => $request->input('description'),
    'product_price' => str_replace(",", ".", $request->input('product_price')) ?? 0,
    'promo_product_price' => str_replace(",", ".", $request->input('promo_product_price')) ?? 0,
    'product_delivery_price' => str_replace(",", ".", $request->input('product_delivery_price')) ?? 0,
    'quantity' => $request->input('quantity'),
    'url_address' => Str::slug($request->input('name'), '-')
]);

how can I get last insert ID?

My product Migration:

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            //$table->bigInteger('category_id')->unsigned();
            //$table->foreign('category_id')->references('id')->on('product_categories');
            $table->smallInteger('id_delivery_vat')->unsigned();
            $table->foreign('id_delivery_vat')->references('id')->on('vat');
            $table->smallInteger('id_product_vat')->unsigned();
            $table->foreign('id_product_vat')->references('id')->on('vat');
            $table->bigInteger('id_producer')->unsigned();
            //$table->foreign('id_producer')->references('id')->on('product_producers');
            $table->string('name', 120)->nullable();
            $table->string('qr_code', 120)->nullable();
            $table->string('oe_code', 120)->nullable();
            $table->char('enable', 1)->default(0);
            $table->char('promo', 1)->default(0);
            $table->longText('description')->nullable();
            $table->decimal('product_price', 9, 2)->default(0);
            $table->decimal('promo_product_price', 9, 2)->default(0);
            $table->decimal('product_delivery_price', 9, 2)->default(0);
            $table->unsignedInteger('quantity')->default(0);
            $table->string('url_address', 160);
            $table->timestamps();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });

I have bigIncrements for ID.

Model:

class Product extends Model
{
    use scopeActiveTrait;

    protected $fillable = ['company_id', 'id_delivery_vat', 'id_product_vat', 'id_producer', 'name', 'qr_code', 'oe_code', 'enable', 'promo', 'description', 'product_price', 'promo_product_price', 'product_delivery_price', 'quantity', 'url_address'];
    protected $quarded = ['id'];
    public $timestamps = true;

    public function categories()
    {
        return $this->belongsToMany(Category::class, 'product_selected_categories', 'product_id', 'category_id');
    }
}

How can I repair it? I try: $product->id, $product->nextId() - but this Is not working :(

goslscs
  • 187
  • 1
  • 2
  • 10

2 Answers2

0

Here $product->id is the last insert id

sh1hab
  • 661
  • 5
  • 16
0

If you want the last insert id to put it in the next insert product, then you dont have to do it just use auto-increment id.

Else if you want it for another use you can simply save the product and then get the id :

$product->save();
$lastId = $product->id; // or whatever name you give to the id
Maraboc
  • 10,550
  • 3
  • 37
  • 48