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

Laravel pass data from controller to javascript

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

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.

      <html>

      <head>

          <title>GeeksforGeekstitle>

      head>

      <body>

          <h2>My Articleh2>

          <h2>{{ $articleName }}h2>

      body>

      html>

      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:

    Laravel pass data from controller to javascript

    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.

      <html>

      <head>

          <title>GeeksforGeekstitle>

      head>

      <body>

          <h2>My Articleh2>

          <ul>

              @foreach ($articles as $article)

                  <li>{{ $article }}li>

              @endforeach

          ul>

      body>

      html>

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

    Output:

    Laravel pass data from controller to javascript

    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.

      <html>

      <head>

          <title>GeeksforGeekstitle>

      head>

      <body>

          <h2>My Articleh2>

          <p>{{ $articleName }}p>

          <p>{{ $articlePublished }}p>

      body>

      html>

      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:

    Laravel pass data from controller to javascript

    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.

      <html>

      <head>

          <title>GeeksforGeekstitle>

      head>

      <body>

          <h2>My Articleh2>    

          @foreach ($articleName as $article)

              <li>{{ $article }}li>

          @endforeach

          <p>{{ $articlePublished }}p>

      body>

      html>

      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:
        Laravel pass data from controller to javascript

        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.

          namespace App\Http\Controllers;

          use Illuminate\Http\Request;

          class GfGController extends Controller

          {

              public function article() {

                  return view('gfg', ['article' =>

                            'Passing Data to View']);

              }

          }

        3. Now, write the below code in the ‘web.php’ file.
          Route::get('/', 'GfGController@article');
          

          In this, the first parameter is the route and the second is the controller name with the function name to handle the route which is separated by ‘@’.

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

          <html>

          <head>

              <title>GeeksforGeekstitle>

          head>

          <body>

              <h2>My Articleh2>

              <p>{{ $article }}p>

          body>

          html>

          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:
            Laravel pass data from controller to javascript

            PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.