0

The variable current_restroom_id in the script cannot be passed to the Controller in Laravel.

console.log('{{HomeController::getTrendingIssue('1',current_restroom_id)}}'); 
// but this is not passing script variable value. 

Please help to resolve this.

Here is the related code in blade file:

<div class="p-t-20">
        <div class="row">
            <div class="col s12">

                <style>
                    #chartdiv3 {
                        width: 100%;
                        height: 500px;
                    }
                </style>

                <!-- Chart code -->
                <script>

                var current_restroom_id = '14';

                am4core.ready(function() {

                // Themes begin
                am4core.useTheme(am4themes_animated);
                // Themes end

                var chart = am4core.create("chartdiv3", am4charts.PieChart3D);
                chart.hiddenState.properties.opacity = 0; // this creates initial fade-in

                console.log('{{HomeController::getTrendingIssue('1', '14')}}'); // this works fine            console.log('{{HomeController::getTrendingIssue('1',current_restroom_id)}}'); // but this is not passing script variable value.
                console.log(current_restroom_id); // This prints 14 in console

                chart.data = [
                    {
                    "issues"    :'{{HomeController::getIssueName('1')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('1', current_restroom_id)}}'
                    },
                    {
                    "issues"    :'{{HomeController::getIssueName('2')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('2', current_restroom_id)}}'
                    },
                    {
                    "issues"    :'{{HomeController::getIssueName('3')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('3', current_restroom_id)}}'
                    },
                    {
                    "issues"    :'{{HomeController::getIssueName('4')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('4', current_restroom_id)}}'
                    },
                    {
                    "issues"    :'{{HomeController::getIssueName('5')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('5', current_restroom_id)}}'
                    },
                    {
                    "issues"    :'{{HomeController::getIssueName('6')}}',
                    "dislikes"  :'{{HomeController::getTrendingIssue('6', current_restroom_id)}}'
                    }
                ];
                </script>
                <div id="chartdiv3"></div>
            </div>
        </div>
    </div>

Here is the HomeController function:

public static function getTrendingIssue($issue_id, $restroom_id)

{
   $issue_count = DB::table('ticket_manages')
                    ->where(['ticket_manages.issue_id' => $issue_id, 'restroom_id' => $restroom_id])
                    ->count();
   return $issue_count;
}
3
  • please explain better what are you trying to do, cause I am not able to understand your problem Commented Aug 11, 2019 at 9:28
  • @GiacomoM As the post says, I'm trying to pass "current_restroom_id" to the controller but '{{HomeController::getTrendingIssue('1', current_restroom_id)}}' cannot recognise variable current_restroom_id from the script. Commented Aug 11, 2019 at 9:56
  • what are you saying it does not have sense: you want to pass a variable from the template file to the controller, but laravel works in the opposite side. From the controller to the template file. Commented Aug 11, 2019 at 9:57

1 Answer 1

1

Do not call controller method statically from blade view!

You should pass the variable via controller to view.

Do not query mysql from controller. This is the model. You should use mysql get via model, and you have to call it from the controller.

The HomeController:

public function index()
{
   $restroom_id = 123; // for example
   $trendingIssue = DB::table('ticket_manages')
                    ->where(['ticket_manages.issue_id' => $issue_id, 'restroom_id' => $restroom_id])
                    ->count();
   return view('home', compact('trendingIssue'));
}

In the view:

{{$trendingIssue}}

But you tried to get via javascript. You cannot call directly PHP from JavaScript. If you want to do it dynamically you have to use AJAX.

            "dislikes"  :'{{HomeController::getTrendingIssue('1', current_restroom_id)}}'

It is never will works, because you cannot pass to PHP the JS variable. The PHP code run SERVER SIDE before the javascript run CLIENT SIDE. This PHP code will try use current_restroom_id const variable, which is not exists.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.