Different ways for passing data to view in Laravel

01 Aug, 2021
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:
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.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
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.
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.
Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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:
Example 2: We can pass an array with multiple values also.
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]);
});
Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!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>Note: The output is going to be the same for both methods.
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]);
});

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]);
});

Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!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>Note: The output is going to be the same for both methods.
<!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>
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:
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.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
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.
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.
Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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:
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:
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.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!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>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: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:We first have to create a controller class by running the command below on the command line.php artisan make:controller GfGControllerAfter 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.<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;  class GfGController extends Controller{    public function article() {        return view('gfg', ['article' =>                  'Passing Data to View']);    }}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 ‘@’.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.Output: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.My Personal Notes
arrow_drop_upSave
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.
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.
Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!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>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.
<!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>
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: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:We first have to create a controller class by running the command below on the command line.php artisan make:controller GfGControllerAfter 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.<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;  class GfGController extends Controller{    public function article() {        return view('gfg', ['article' =>                  'Passing Data to View']);    }}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 ‘@’.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.Output: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.My Personal Notes
arrow_drop_upSave
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:
We first have to create a controller class by running the command below on the command line.php artisan make:controller GfGControllerAfter 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.<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;  class GfGController extends Controller{    public function article() {        return view('gfg', ['article' =>                  'Passing Data to View']);    }}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 ‘@’.Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.Output: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.My Personal Notes
arrow_drop_upSave
We first have to create a controller class by running the command below on the command line.php artisan make:controller GfGController
php artisan make:controller GfGController
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.<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;  class GfGController extends Controller{    public function article() {        return view('gfg', ['article' =>                  'Passing Data to View']);    }}
<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;  class GfGController extends Controller{    public function article() {        return view('gfg', ['article' =>                  'Passing Data to View']);    }}
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 ‘@’.
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 ‘@’.
Create and write the below code in the ‘gfg.blade.php’ file in ‘resources/views’ directory.<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
<!DOCTYPE html><html><head>    <title>GeeksforGeeks</title></head><body>    <h1>My Article</h1>    <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.
Output: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.My Personal Notes
arrow_drop_upSave
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.
Laravel
PHP
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?