【发布时间】:2026-02-05 09:35:01
【问题描述】:
我将 Laravel 与 Sweetalert 一起使用,并尝试显示一条自定义弹出消息,该消息显示 Laravel 变量的值:
$(document).on('click', '#custombtn', function(e) {
e.preventDefault();
let form = $(this).parents('form');
swal(
{
title: "Alert!",
text: "Youre wallet balance is: {!! digits2persian(json_decode($user_wallet->balance)) !!}",
type: "warning",
allowEscapeKey: false,
allowOutsideClick: false,
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
showLoaderOnConfirm: true,
closeOnConfirm: false
}).then((isConfirm) => {
if (isConfirm.value === true) {
window.location.href = {!! json_encode(route('courseRegistrationWithWallet', ['course'=>$item->cor_id,'wallet'=>$user_wallet->id ?? ''])) !!}
}
return false;
});
});
但是现在的问题是,当$user_wallet->balance返回null时,就会出现这个错误:
试图获取非对象的属性“平衡”
所以我尝试这个来显示 empty 结果,如果变量没有设置:
{!! digits2persian(json_decode($user_wallet->balance)) ?? '' !!}
但是没有解决这个问题,仍然出现错误!
那么如何解决这个问题呢?
【问题讨论】:
-
你可以修改你的条件语句为
{!! $user_wallet->balance ? digits2persian(json_decode($user_wallet->balance)) : '' !!}
标签: php laravel laravel-5.8