Ganesh Ganesh - 1 month ago 14
PHP Question

How to access DB values outside of try catch block in laravel 5.3

i am using laravel 5.3,in that i have try catch block.In that try block i performed insert operation using eloquent query. Now i want to access the id of transaction, outside of the try catch block the code as follow,

function sample(){
try
{
$User->name=$username;
}
catch(){}
return redirect::route('edit',$User->id);
}


Where id is set as auto increment, when I call $User->id it returns an empty value. How do i access it?

Answer

You have to save your data first as

function sample(){   
   try
   {
       $User->name=$username;
   }
   catch(){}
   if($User->save()) {
       return redirect::route('edit',$User->id);
   }
}

After save, $User->id should be the last id inserted.