Ganesh Ganesh - 1 month ago 7
PHP Question

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

I'm 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.

Comments