Laravel pass data from controller to javascript

I want to pass data from my Controller to a JavaScript that handles a Google Bar Chart.

composer

$tmp = 6;
return view['pages.template', ['tmp' => $tmp]];

from my template.blade.php I call the Google Chart

.js file:

var tmp = 6;
var tmp2 = parseInt['{!! $tmp !!}'];

var data = google.visualization.arrayToDataTable[[
    ['comment1', 'comment2'],
    ["- 6 days", tmp],
    ["- 5 days", tmp2],
    ["- 4 days", 31],
    ["- 3 days", 12],
    ["- 2 days", 10],
    ["- 1 day", 3],
    ["today", 4]

the 2nd bar from Google-Bar-Chart is blank ....

But there should be displayed my variable.

How do I solve this issue?

In this tutorial I will show you how to send value from controller to Javascript in Blade in Laravel application.

Say you have a value $x=10 in your controller and you want to grab the value in your blade. Then do the following steps to get the value in front end code.

1. Send the value using compact function to blade like below

public function Posts[]{
    ....................
    .....................
    $x=10;
   ......................
   return view['my_view', compact['x']];
}

2. Now in your javascript retrieve the value like below


  let x = "{{$x}}";
  /*
    Now you can print it or use the way you want.
  */
  console.log[x];

3. In the above javascript function $x has the value passed from laravel controller.

In this small article, I will let you know how to pass a variable to jquery in laravel. We always require to get data from laravel model eloquent and pass that variable to javascript.

We almost require to pass array variable to javascript in laravel like calender, chart, google map etc. So when you require to print php variable in js then you can do it easily.

Here, i will simple get all users from User model then i will simple assign to users variable of jquery. So i want to just share with you controller method and how you can access that variable in jquery.

Controller Method:

public function index[]

{

$users = User::get[];

return view['users.index', compact['users']];

}

Access Variable to JS:

var users = {!! json_encode[$users->toArray[]] !!};

console.log[users];

I hope you got it and you can do it this way....

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Laravel provides different ways to pass data to a view. We can pass data directly from routes or through the controller.

    Here are some of the ways we can pass data to the view:

    • Using view[]
    • Using with[]
    • Using compact[]
    • Using Controller Class

    1. Using view[]: We can directly pass the data in the ‘view[]’ helper function by using the second parameter in the function which takes an array as key and value pair.

    Note: Comment or delete any previous route in the ‘web.php’ file in ‘routes’ directory.

    Example 1:

    1. Write the below code in the ‘web.php’ file.
      Route::get['/', function [] {
          return view['gfg', ['articleName' => 'Article 1']];
      }];
      

      In view[], the first parameter is the name of the view and the second parameter is where we have to specify the key and value pair of the data.

    2. Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.

          GeeksforGeeks

          My Article

          {{ $articleName }}

      Here we have used double curly brackets ‘{{ }}’ which is to escape HTML elements. In that, we have specified the name of the key, as a PHP variable, that we passed in the ‘view’ helper function.

    Output:

    Example 2: We can pass an array with multiple values also.

    1. Write the below code in the ‘web.php’ file.
      Route::get['/', function [] {
          return view['gfg', [ 'articles' => 
            ['Article 1','Article 2','Article 3'] ]];
      }];

      In view[], the first parameter is the name of the view and the second parameter is where we have specified one key and multiple values of the data.

      We can also store the array elements in a variable and then pass it in the view function. In the code below we have stored all the values in a variable and passed it in the second parameter of view[] function as a value.

      Route::get['/', function [] {
         $articles = ['Article 1','Article 2','Article 3'];
         return view['gfg', ['articles' => $articles]];
      }];
      
    2. Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.

          GeeksforGeeks

          My Article

          

              @foreach [$articles as $article]

                  {{ $article }}

              @endforeach

          

      Note: The output is going to be the same for both methods.

    Output:

    2. Using with[]: The ‘with[]’ is a method to pass individual form of data and is used with the ‘view’ helper function.
    Note: Comment or delete any previous route in the ‘web.php’ file in ‘routes’ directory.

    Example:

    1. Write the below code in the ‘web.php’ file.
      Route::get['/', function [] {
         $articleName = ‘Article 1’;
         return view['gfg']->with['articleName', $articleName]->
                      with['articlePublished', 'On GeeksforGeeks'];
      }];

      As you can see in the above code, we have to specify an arrow ‘->’ and then use the ‘with[]’ method with ‘view[]’ helper function. And we can specify one or more ‘with[]’ depending on the requirement and use case.

    2. Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.

          GeeksforGeeks

          My Article

          {{ $articleName }}

          {{ $articlePublished }}

      Here we have used double curly brackets ‘{{ }}’ which is to escape HTML elements. In that, we have specified the name of the key, as a PHP variable, that we passed in the ‘view’ helper function.

    Output:

    3. Using compact[]: The ‘compact[]’ is a PHP function that can be used for creating an array with variable and there value. Here the name on the variable is the key and the variable content is the value.

    Note: Comment or delete any previous route in the ‘web.php’ file in ‘routes’ directory.

    Example:

    1. Write the below code in the ‘web.php’ file.
      Route::get['/', function [] {
          $articleName = ['Article 1','Article 2'];
          $articlePublished = 'On GeeksforGeeks';
          return view['gfg', compact['articleName', 
                               'articlePublished']];
      }];

      In view[], the first parameter is the name of the view and second is where we have to specify the ‘compact[]’ function.

    2. Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.

          GeeksforGeeks

          My Article    

          @foreach [$articleName as $article]

              {{ $article }}

          @endforeach

          {{ $articlePublished }}

      Here we have used double curly brackets ‘{{ }}’ which is to escape HTML elements. In that, we have specified the name of the key, as a PHP variable, that we passed in the ‘view’ helper function.

      1. Output:

        4. Using Controller Class: Passing data using controller class is easy and is the right way.
        Note: Comment or delete any previous route in the ‘web.php’ file in ‘routes’ directory.

        Example:

        1. We first have to create a controller class by running the command below on the command line.
          php artisan make:controller GfGController
        2. After that, open the ‘GfGController.php’ file in ‘app/Http/Controllers’ directory and create a public function named ‘article’. In this function we can specify any of the data passing method we saw above.

    Chủ Đề