Skip to content
Advertisement

Console.log does not show result based on variable value

I’m using Laravel 5.8 and in this project, I wanted to show some results in Javascript based on the variable which is sent to View.

So at the Controller, I have added this:

$title = "";
if($popup->showtitle == 1){
    $title = $popup->title;
}

return view("frontend.home")->with('title',$title);

Then at the view:

<script>
   var title = JSON.parse("{{ json_encode($title) }}");
   if(!title){
        console.log(1);
   }else{
        console.log(2);
   }
</script>

So basically, if title does not have any value, it should be showing 1 at the Console bar, otherwise 2 must be appears.

But the problem is, it does not show anything at all!

So what’s going wrong out there? How can I properly get the result based on this variable value?

Advertisement

Answer

in controller

    $title = "your title";
    return view('rontend.home')->with([
        'title' => $title,
    ]);

in blade

<script>
    $(document).ready(function() {
    var title = {!! json_encode($title) !!};
    console.log(title);
    });
</script>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement