我在访问刀片中的数据时在PHP Laravel中收到此错误。
使用未定义的常量user_id - 假设为'user_id'(这将在未来的PHP版本中引发错误)(查看:/home/beatsbaj/public_html/resources/views/user/reset-password.blade.php)
#Here is My Controller's function
public function resetPassword()
{
try
{
$token_id = Crypt::decrypt(Request::segment(2));
$token = Crypt::decrypt(Request::segment(3));
$data = Remembertoken::select('id', 'token_id', 'token', 'user_id')->where('token_id', $token_id)->get()->first();
if(!$data)
{
return abort(404);
}
else if($data)
{
if($data->token != $token)
{
return abort(404);
}
else if($data->token == $token)
{
return view('user.reset-password', ["token_id"=>$token_id, "user_id"=>$data->user_id]);
}
}
}
catch(\Exception $e)
{
abort(404);
}
}
当我访问user_id变量时,我的刀片中的Ajax部分。
<script>
$(document).ready(function(){
$("#reset-pass-form").submit(function(event) {
event.preventDefault();
var form = $(this);
form = form.serialize(form);
$.ajax({
url:"{{url('/reset-password-now')}}",
type:"post",
data:form,
success:function(response){
console.log(response);
console.log("{{user_id}}"); // Getting Error Here
},
error:function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
},
});
});
});
</script>
它假设为“{{$ user_id}}”而不是{{user_id}}。请注意差异$
。
此外,为什么要向用户抛出异常?我认为你应该有一个默认的错误页面或只是重定向到另一个页面(或上一页)
为什么我收到此错误?
没有任何$
的变量称为常量。必须首先定义常量:http://php.net/manual/en/language.constants.php
解决方案
您的视图中缺少$
:
console.log("{{ $user_id }}");