Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading