如何在甜美警报消息中显示JSON编码阵列
我正在使用laravel 8和 sweet Alert ,我想显示被抓住的错误消息通过错误例外,作为甜警告错误弹出消息。
基本上,这是我的控制器方法:
try{
...
}catch(\Exception $e){
// Alert::error('Error Title', 'Error Message');
return json_encode(['status' => '500', 'msg' => __('message.error-server')]);
}
因此,如您所见,我已经编码了一个关联数组,该数组包含错误消息的信息,但我不想返回
IT。实际上,我必须将其显示为alert ::错误(...)
。
那我该怎么做呢?
更新1:
我只是对此进行了测试,但没有向我显示错误:
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();
}
更新2 :
我只是尝试了此错误,但没有发现错误例外,并向我展示了<代码>警报::成功(...)而不是。
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();
}
更新#3:
我最终可以获取错误:
但我想显示$ attributes ['status']
是> 500 作为错误标题,该错误的主体包含$属性['msg']
。我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行此操作:
基本上是在做什么,它将将关联数组转换为字符串,然后您可以在您的警报中使用最终字符串IE
$ aSSICIATIVESTRING
AS:这将输出:您可以:
您可以 :修改
返回$ key。'=&gt;'。$ value;
内部映射中以按所需的方式塑造最终输出。更新#1
相信它遵循
alter的语法::
查看您使用的SweetAlert文档,我 此:
更新#2
更新#3
您应该致力于改善PHP中的阵列概念以及它们的工作方式 PHP关联阵列如何工作现在,根据您的要求,您不应将关联数组转换为我原始答案中建议的字符串。试试看:
alert :: error()
采用两个参数,第一个是标题,第二个是消息,您只需从关联阵列的相关键中获取值,即>状态
和msg
在我们的情况下。You can do this:
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:this will output like:
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:to this:
UPDATE #2
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:
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
andmsg
in our case.这应该起作用
This Should Work