larasad's avatar

undefined variable view

Hello guys, So, bought a php script on some site, and for the life of my I cannot reach the dev. I scourged google, but I have no understanding of laravel... I have 2 bugs that stop me from using the script, so I ask for some help here...

It seems I have 2 variables which are undefined and won't allow me to login, neither as a user, nor as admin.

code1 and coins

First:

Undefined variable: code1 (View: C:\laragon\www\ventz\app\resources\views\auth\login.blade.php)

My RedirectifAutenthicated.php

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('user/home');
        }
        return $next($request);
    }
}

My login.blade.php

   <? $code1 = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890') , 0 , 5 );?>
                                 <div class="form-group">
                                 <b>Code: {{$code1}}</b>
                                   <script>
                                    function myFunction() {
                                     var code = $('#usd').val() ;
                                     var a = "{{$code1}}";
                                     if(code ==  a){
                                     document.getElementById("show").innerHTML = "<br><button type='submit' class='btn btn-bg--primary btn-size--md btn-hover--3d'>Sign In</button>"; }

                                     else
                                     {

                                      document.getElementById("show").innerHTML = "<p class='text-danger'>Please enter a correct code to activate login button</p>";
                                     }
                                     };
                                    </script>

                                    <input type="email" class="form-control" id="usd" onkeyup="myFunction()"   placeholder="Enter Code" required/>

And second :coins

Undefined variable: coins (View: C:\laragon\www\ventz\app\resources\views\include\admindashboard.blade.php) 

My admindashboard.blade.php

<? $coins = DB::table('coins')->get(); ?>
@foreach($coins as $data)
<li><a href="{{route('admin.blockchainwallet', $data->id)}}"><em class="text-primary cf cf-coin"></em>&nbsp;  {{$data->name}}</a></li>
@endforeach

Could you please give some hints? Can this be fixed? Thank you

0 likes
5 replies
SilenceBringer's avatar
Level 56

Hi @larasad first of all, it's very bad to perform database query in your view. I mean this one

<? $coins = DB::table('coins')->get(); ?>

and about your problem - possible it's related to short tags?

try

<?php $code1 = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890') , 0 , 5 ); ?>

or

@php
    $code1 = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890') , 0 , 5 );
@endphp
1 like
larasad's avatar

You are amazing! This solved both of my issues. Anyway, this code seems poorly written, but you mean querying database in view is bad securitywise? Thank you once again!!!! You made my day!

SilenceBringer's avatar

@larasad view must be responsible for data displaying, not interacting with the database. Call this in your controller and then pass the data to view

Please, consider marking response as "best answer" if it's solved your problem

larasad's avatar

I am sorry, that was a missclick. Thank you!

Please or to participate in this conversation.