1

I am getting this error in PHP Laravel while accessing data in blade.

Use of undefined constant user_id - assumed 'user_id' (this will throw an Error in a future version of PHP) (View: /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);
        }

    }

My Ajax part in blade when I am accessing user_id variable.

<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>
Riosant
  • 344
  • 3
  • 15
  • 1
    `console.log("{{user_id}}");` should be `console.log("{{$user_id}}");`. If you omit the `$` in front of a variable, it will be considered a constant. Since you don't have a constant called `user_id`, PHP assumes it's a string and will output the literal text `user_id`. That's the behavior that may change in future versions. – M. Eriksson Sep 13 '18 at 08:36
  • Oh My God! I cannot believe I did it. Thank you so much. – Riosant Sep 13 '18 at 08:39
  • 1
    Here's a good SO post about all kinds of error messages. It's a good page to bookmark [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/). In this case, [here's the answer](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12773272#12773272). I would have marked it as a duplicate, but I've already voted to close this as a typo-question. – M. Eriksson Sep 13 '18 at 08:40

2 Answers2

1

It suppose to be '{{ $user_id }}' not {{ user_id }}. Note the difference $.

Furthermore, Why would you want to throw exception to your users? I think you should have a default error page or simply redirect to another page (or previous page)

1

Why am I getting this error?

A variable without any $ is called a constant. A constant must be defined first: http://php.net/manual/en/language.constants.php

The solution

There is a missing $ in your view:

console.log("{{ $user_id }}");
thisistg
  • 190
  • 8