jeudi 26 octobre 2017

getting the last inserted id from laravel with the create method

I am trying to get the last inserted id from the laravel controller that was calling using the following code..

$invoiceData = array(
            'client_id'     => $request->get('client'),
            'number'        => $request->get('number'),
            'invoice_date_entry'  => date('Y-m-d', strtotime($request->get('invoice_date_entry'))),
            'invoice_date'  => date('Y-m-d', strtotime($request->get('invoice_date'))),
            'due_date'      => date('Y-m-d', strtotime($request->get('due_date'))),
            'notes'         => $request->get('notes'),
            'terms'         => $request->get('terms'),
            'currency'      => $request->get('currency'),
            'status'        => $request->get('status'),
            'discount'      => $request->get('discount') != '' ? $request->get('discount') : 0
        );
        $invoice = $this->invoice->create($invoiceData);

In the above code gets executed, row is inserted in the database but somehow i am not getting the last inserted id,

echo $invoice->id;

somehow, I am getting NULL as the result,

This is my Model:

<?php namespace App\Models;


class Invoice extends BaseModel {

    /**
     * Main table primary key
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * @var array
     */

    protected $fillable = ['client_id', 'number', 'invoice_date', 'due_date', 'status', 'discount', 'terms', 'notes', 'currency'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */

    public function client(){
        return $this->belongsTo('App\Models\Client');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function items(){
        return $this->hasMany('App\Models\InvoiceItem');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function payments(){
        return $this->hasMany('App\Models\Payment');
    }


}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2zQuAmX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire