Reputation: 2547
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;
}
Upvotes: 0
Views: 164
Reputation: 21
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.
Upvotes: 1