Reputation: 19207
I'm trying to get the last insert ID like so:
$leadData = [
'first_name' => $first_name,
'last_name' => $last_name,
'phone_number' => $phone_number,
'company' => $company,
'email_address' => $email_address,
'iso_code' => $iso_code,
'product' => $product,
'transaction_id' => $transaction_id,
'rep_name' => $rep_name,
'ip_address' => $ip_address
];
//insert lead into DB
$lead = new Lead();
$lead->create($leadData);
//dd($lead);
$leadId = $lead->id;
dd($leadId);
but $leadId is NULL every time. My record is being inserted correctly into the MySQL table. Its a seemingly stupid problem, but has had me stumped for the last hour.
How could $leadID possibly be null?
Upvotes: 0
Views: 1723
Reputation: 499
Just keep create object to one variable and print this print this variable object
//insert lead into DB
$lead = new Lead();
$lead->create($leadData);
//dd($lead);
$leadId = $lead->id;
dd($leadId);
to
$lead = new Lead();
$leadData = $lead->create($lead);
//dd($leadData);
$leadId = $leadData->id;
dd($leadId)
Upvotes: 0
Reputation: 19207
I figured it out, I had to first assign the return value of ->create() to a variable, and then use the ->id method as in my first example.
changing
$lead = new Lead();
$lead->create($leadData);
$leadId = $lead->id;
to
$lead = new Lead();
$result = $lead->create($leadData);
$leadId = $result->id;
Upvotes: 1