Different ways for passing data to view in Laravel

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <head>
        <title>GeeksforGeeks</title>
    </head>
    <body>
        <h1>My Article</h1>
        <h2>{{ $articleName }}</h2>
    </body>
    </html>

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <head>
        <title>GeeksforGeeks</title>
    </head>
    <body>
        <h1>My Article</h1>
        <ul>
            @foreach ($articles as $article)
                <li>{{ $article }}</li>
            @endforeach
        </ul>
    </body>
    </html>

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <head>
        <title>GeeksforGeeks</title>
    </head>
    <body>
        <h1>My Article</h1>
        <p>{{ $articleName }}</p>
        <p>{{ $articlePublished }}</p>
    </body>
    </html>

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <head>
        <title>GeeksforGeeks</title>
    </head>
    <body>
        <h1>My Article</h1>    
        @foreach ($articleName as $article)
            <li>{{ $article }}</li>
        @endforeach
        <p>{{ $articlePublished }}</p>
    </body>
    </html>

    chevron_right

    
    

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

        edit
        close

        play_arrow

        link
        brightness_4
        code

        <?php
          
        namespace App\Http\Controllers;
          
        use Illuminate\Http\Request;
          
        class GfGController extends Controller
        {
            public function article() {
                return view('gfg', ['article' =>
                          'Passing Data to View']);
            }
        }

        chevron_right

        
        

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

        edit
        close

        play_arrow

        link
        brightness_4
        code

        <!DOCTYPE html>
        <html>
        <head>
            <title>GeeksforGeeks</title>
        </head>
        <body>
            <h1>My Article</h1>
            <p>{{ $article }}</p>
        </body>
        </html>

        chevron_right

        
        

        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:




          My Personal Notes arrow_drop_up

          Check out this Author's contributed articles.

          If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

          Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


          Article Tags :
          Practice Tags :


          Be the First to upvote.


          Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.