I'm using Laravel 8 and Sweet Alert and I wanted to show the error messages that were caught by error Exception as Sweet Alert error popup message.
Basically here is my Controller method:
try{
...
}catch(\Exception $e){
// Alert::error('Error Title', 'Error Message');
return json_encode(['status' => '500', 'msg' => __('message.error-server')]);
}
So as you can see I have json encoded an associative array that holds the information of the error message but I don't want to return it. In fact I have to show it as Alert::error(...).
So how can I do that?
UPDATE 1:
I just tested this but not showing me the error as Alert:
public function destroy(User $user)
{
try{
$useradasd->is_active = 0;
$useradasd->is_deleted = 1;
$useradasd->remover_id = Auth::id();
$useradasd->save();
}catch(\Exception $e){
$attributes = ['status' => '500', 'msg' => __('message.error-server')];
$dataAttributes = array_map(function($value, $key) {
return $key.'=>'.$value;
}, array_values($attributes), array_keys($attributes));
$associativeString = implode(', ', $dataAttributes);
Alert::error($associativeString);
}
Alert::success('Removed', 'That user is deleted');
return back();
}
UPDATE 2:
I just tried this, but does not catch the error exception and show me the Alert::success(...) instead.
public function destroy(User $user)
{
try{
$useradasd->is_active = 0;
$useradasd->is_deleted = 1;
$useradasd->remover_id = Auth::id();
$useradasd->save();
}catch(\Exception $e){
$attributes = ['status' => '500', 'msg' => __('message.error-server')];
$dataAttributes = array_map(function($value, $key) {
return $key.'=>'.$value;
}, array_values($attributes), array_keys($attributes));
$associativeString = implode(', ', $dataAttributes);
Alert::error('Error',$associativeString);
}
Alert::success('Removed', 'That user is deleted');
return back();
}
UPDATE #3:
I can finally get the error:
But I wanted to show $attributes['status'] which is 500 as Error Title and the body of that error contains $attributes['msg']. How can I do that?
You can do this:
$attributes = ['status' => '500', 'msg' => __('message.error-server')
$dataAttributes = array_map(function($value, $key) {
return $key.'=>'.$value;
}, array_values($attributes), array_keys($attributes));
$associativeString = implode(', ', $dataAttributes);
What this does basically is, it will convert the associative array to string first and then you can use the final string i.e.$associativeString in your alert as:
Alert::error($associativeString);
this will output like:
status => 500, msg => Internal Server Error
you can modify return $key.'=>'.$value; inside map to shape the final output the way you want.
UPDATE #1
Looking at the SweetAlert docs you used, I believe it follows the syntax of Alert::[type]([Title],[message]), You can update the alert from this:
Alert::error($associativeString);
to this:
Alert::error('Error',$associativeString);
UPDATE #2
public function destroy(User $user)
{
try{
$useradasd->is_active = 0;
$useradasd->is_deleted = 1;
$useradasd->remover_id = Auth::id();
$useradasd->save();
throw new Exception("Custom Exception from try block");
}catch(\Exception $e){
$attributes = ['status' => '500', 'msg' => $e->getMessage()];
$dataAttributes = array_map(function($value, $key) {
return $key.'=>'.$value;
}, array_values($attributes), array_keys($attributes));
$associativeString = implode(', ', $dataAttributes);
Alert::error('Error',$associativeString);
}
return back();
}
UPDATE #3
You should work on improving your concepts of arrays in php and how they work learn How PHP associative arrays work, now according to your requirement you should not convert the associative array to string as I suggested in my original answer. try this instead:
public function destroy(User $user)
{
try{
$useradasd->is_active = 0;
$useradasd->is_deleted = 1;
$useradasd->remover_id = Auth::id();
$useradasd->save();
throw new Exception("Custom Exception from try block");
}catch(\Exception $e){
$attributes = ['status' => '500', 'msg' => $e->getMessage()];
Alert::error($attributes['status'],$attributes['msg']);
}
return back();
}
the Alert::error() takes two parameters the first one is Title and the second is the message, you just have to fetch the value from related keys of associative array i.e. status and msg in our case.
This Should Work
Alert::error(json_encode(['status' => '500', 'msg' => __('message.error-server')],JSON_PRETTY_PRINT));
Related
I am building a API using laravel, and i am trying to create a function to find a user in data base using a email as a parameter
public function find_user(Request $request){
try{
$user = UserAccess::table('user_accesses')->where('email', $request->email)->first();
return ['api_request_return'=>'ok', 'return' => $user];
}catch(\Exception $error){
return ['api_request_return' => 'error', 'error_details' => $error];
}
}
The API route is ok, but its always returning error:
{
"api_request_return": "error",
"error_details": {}
}
Use the corrected one
public function find_user(Request $request){
try{
$user = DB::table('user_accesses')->where('email', $request->email)->first();
return ['api_request_return'=>'ok', 'return' => $user];
}catch(\Exception $error){
return ['api_request_return' => 'error', 'error_details' => $error];
}
}
I have the following blocks of code
try {
$data = Checklist::where('setupStatus', true)->whereJsonContains('workspaceRegions', $regionID)->orderBy('id', 'desc')->paginate(5);
return response()->json([
'data' => $data
], 200);
} catch (\Throwable $th) {
return $th;
}
This works as expected and returns the correct checklists.
So then I did the same in the next block, but this time I have to iterate through and array to get the id values to search for. This always return an empty array though
$company = Auth::user()->company;
try {
$hodIDs = CompanyRoles::where('hod', true)->get();
$hods = [];
foreach ($hodIDs as $option) {
$id = strval($option->id);
$hods = User::where('companyID', $company->id)->whereJsonContains('companyRoles', $id)->with('employee')->get();
}
return response()->json([
'message' => 'success',
'hods' => $hods
], 200);
} catch (\Throwable $th) {
return $th;
}
The 'companyRoles' column is an array that contains something like ["1","2"]
How can I return each user with and hodID within the companyRoles column array?
The following gives me what I want, but this feels a bit like a hack.
$hods = [];
foreach ($hodIDs as $option) {
$id = strval($option->id);
$user = User::where('companyID', $company->id)->whereJsonContains('companyRoles', $id)->with('employee')->first();
if ($user) {
array_push($hods, $user);
}
}
Thanking you in advance
I have a function
public function getCandidates($candidateEmail)
{
try {
$moduleIns = ZCRMRestClient::getInstance()->getModuleInstance('Candidats');
$response = $moduleIns->searchRecordsByEmail($candidateEmail, 1, 1);
$candidates = $response->getResponseJSON();
return $candidates;
} catch (ZCRMException $e) {
echo $e->getMessage();
echo $e->getExceptionCode();
echo $e->getCode();
}
}
And I use this function like that :
$obj = new ZohoV2();
$response = $obj->getCandidates($request->email);
$candidate = $response['data'][0];
return response()->json([ 'status' => 'success', 'candidate' => $candidate ], 200);
Theses functions allows me to retrieve a user from a database of a CRM.
But when the user does not exist, he sends me a 500 error.
{message: "No Content", exception: "zcrmsdk\crm\exception\ZCRMException",…}
exception: "zcrmsdk\crm\exception\ZCRMException"
file: "/home/vagrant/CloudStation/knok/myath/myath-app/vendor/zohocrm/php-sdk/src/crm/api/response/BulkAPIResponse.php"
line: 61
message: "No Content"
trace: [{,…}, {,…}, {,…}, {,…}, {,…}, {,…},…]
How to intercept this error so that I can process it as I want and send an error message ?
Thank you
Remove the try/catch from your first code block
public function getCandidates($candidateEmail)
{
$moduleIns = ZCRMRestClient::getInstance()->getModuleInstance('Candidats');
$response = $moduleIns->searchRecordsByEmail($candidateEmail, 1, 1);
$candidates = $response->getResponseJSON();
return $candidates;
}
And move it to the second code block (I assume it's the controller)
$obj = new ZohoV2();
try {
$response = $obj->getCandidates($request->email);
} catch (ZCRMException $e) {
return response()->json(['status' => 'failed', 'error' => $e->getMessage()], 404);
}
$candidate = $response['data'][0];
return response()->json([ 'status' => 'success', 'candidate' => $candidate ], 200);
I need to transfer data from Mysq table (Paniers) to another Mysql table (Commandes) and delete the data from first table after transfer.
Here is my code:
function Commande(Request $request) {
$pn = $request->input('id');
$pdr = Panier::find($pn);
$user = Commande::create([
'ID_User' => $pdr->ID_User,
'ID_Piece' => $pdr->ID_Piece,
'QTE' => $pdr->QTE,
]);
if($user){
if($pdr->delete())
{
echo 'Commande Confirmée';
}
}
}
I get this error:
"Property [ID_User] does not exist on this collection instance."
If i do this it works but instead of getting all data i only get the first line. I need to get all lines of data!
$pdr = Panier::find($pn)->first();
If $pn is array Panier::find($pn) returns collection not entity so you should iterate it
Panier::find($pn)->each(function($pdr){
$user = Commande::create([
'ID_User' => $pdr->ID_User,
'ID_Piece' => $pdr->ID_Piece,
'QTE' => $pdr->QTE,
]);
if($user){
if($pdr->delete())
{
echo 'Commande Confirmée';
}
}
});
When you are doing :
$pdr = Panier::find($pn);
If the record does not exist, it will return null. Then if you do $pdr->ID_User it is going to throw an error. Please check beloew updates :
<?php
function Commande(Request $request) {
$pn = $request->input('id');
$pdr = Panier::find($pn);
// Model not found
if(!$pdr){
return response()->json(['msg' => 'No records found']);
}
// Create new Commande
$user = Commande::create([
'ID_User' => $pdr->ID_User ?? 'default_value_for_ID_User',
'ID_Piece' => $pdr->ID_Piece ?? 'default_value_for_ID_Piece',
'QTE' => $pdr->QTE ?? 'default_value_for_QTE'
]);
// If user is created
if($user){
// Delete Panier
$pdr->delete();
return response()->json(['msg' => 'Success']);
}
return response()->json(['msg' => 'Could not create new Commande']);
}
For above to work you need to have :
Columns ID_User, ID_Piece and QTE marked as $fillable = [] in Commande Model.
You need to have a basic primary key for Panier Model, otherwise delete() will not work.
You can do it by findOrFail and handling Exception:
function Commande(Request $request) {
$pn = (int) $request->input('id');
try {
$pdr = Panier::findOrFail($pn);
$pdr->each(function ($item, $key) {
$user = Commande::create([
'ID_User' => $item->ID_User,
'ID_Piece' => $item->ID_Piece,
'QTE' => $item->QTE,
]);
if ($user && $item->delete()) {
echo 'Commande Confirmée';
}
});
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
//#Todo handle error
}
}
According to laravel 5.0 documents:
Retrieving A Model By Primary Key Or Throw An Exception
Sometimes you may wish to throw an exception if a model is not found. To do this, you may use the firstOrFail method:
Collection
I have a Laravel application, and on one of the pages, I want to allow the user to be able to update some values in the database, by entering/ changing data in a couple of textboxes.
The Angular function called by the (change) attribute of these HTML textboxes is:
updatePreferredAddresseeDetails($event, payer) {
console.log("updatePreferredAddresseeDetails() called ");
const contact = payer['contacts'][$event.currentTarget.selectedIndex];
payer.loading = true;
payer.originalAddresseeName = payer.addresseename;
payer.originalAddresseeNamePdf = payer.addresseenamepdf;
payer.ADDRESSEENAME = $event.contactPreferredName;
payer.ADDRESSEENAMEPDF = $event.contactPreferredAddresseeName;
this.provService.updatePreferredAddresseeDetails(payer).subscribe(
(response:any) => {
payer.addresseename = response.addresseename;
payer.addresseenamepdf = response.addresseenamepdf;
const message = new Message();
message.type = MessageType.SUCCESS;
message.message = 'Preferred Addressee details have been updated. ';
this.messagingService.emitMessage(message);
payer.loading = false;
},
(error:any) => {
//reset the names back to what they were originally because saving failed
payer.addresseename = payer.originalAddresseeName;
const message = new Message();
message.type = MessageType.ERROR;
message.message = error.message || 'There was a problem updaing the preferred addressee details. If the problem persists, please contact us.';
this.messagingService.emitMessage(message);
payer.loading = false;
}
);
}
The PHP function called by the above Angular function, which should be setting the values in the database is:
public function updatePreferredAddresseeDetails(Request $request)
{
try
{
DB::beginTransaction();
$transactionContactId = $request->input('transactionContactId');
$transactionItemId = $request->input('transactionItemId');
if ($transactionItem = transactionItem::find($transactionItemId))
{
$transaction = $transactionItem->transaction;
if (User::canAccessTransaction( auth()->user()->user, $transaction))
{
$account = Account::find($transaction->accountId);
$account->savePropertyValueByPropertyTag('ADDRESSEENAME', $request->input('contactPreferredName'));
$account->savePropertyValueByPropertyTag('ADDRESSEENAMEPDF', $request->input('contactPreferredAddresseeName'));
$account->save();
DB::commit();
return response()->json([
'success' => true,
'addresseeName' => $account->ADDRESSEENAME,
'addresseeNamePdf' => $account->ADDRESSEENAMEPDF,
]);
}
else
{
return response()->json([
'success' => false,
]);
}
dd("transactionItem: ", $transactionItem);
}
else
{
dd("transactionItem could not be found ");
}
}
catch(Exception $e)
{
$message = $e->getMessage();
if (empty($message))
{
$message = "Preferred addressee details could not be updated. ";
}
DB::rollback();
return response()->json([
'error' => true,
'message' => $message
], 500);
}
}
However, when I enter new values/ update an existing value in one of the textboxes, and then tab out of it, I can see in the browser console that the Angular function is called, and that it in turn calls the PHP function- but in the Network->Preview tab of the console, I see the output:
{success: true, addresseeName: null, addresseeNamePdf: null}
addresseeName: null
addresseeNamePdf: null
success: true
so for some reason, it seems that these values are not actually being updated in the database. Why is this? What am I doing wrong? How can I ensure that the database values are correctly updated from this function?
Edit
Looks like #Devon was possibly right with his comment about the function being used... I had a look the user.php file (which is where canAccessTransaction() is defined), and there was another function: userCanEditAccount(), which I think is probably the one I want. It's defined with:
private static function userCanEditAccount($userId, $accountId)
{
return Account::canUserEditAccount( $userId, $accountId );
}
so I changed that part inside the updatePreferredAddresseeDetails() function to:
if(User::userCanEditAccount( $request->userId, $request->accountId)
{
$account = Account::find($request->accountId);
$account->savePropertyValueByPropertyTag('ADDRESSEENAME', $request->input('contactPreferredName'));
$account->savePropertyValueByPropertyTag('ADDRESSEENAMEPDF', $request->input('contactPreferredAddresseeName'));
$account->save();
dd("request: ", $request->all());
DB::commit();
return response()->json([
'success' => true,
'addresseeName' => $account->ADDRESSEENAME,
'addresseeNamePdf' => $account->ADDRESSEENAMEPDF,
]);
}
else
{
return response()->json([
'success' => false,
]);
}
But when the page loads now, before I actually interact with it at all, I get an error in the console that says:
Parse error: syntax error, unexpected ';'
on the line
$account = Account::find($request->accountId);
but I'm pretty sure that ; should be there- what else could be causing this?