How to pass data to view in Laravel

How to pass data to view in Laravel

Laravel provides many methods to pass the data to a view. We have different methods to pass the data in Laravel. We can pass the data directly from the controller or through routes.

Laravel provides many methods to do the same task, whichever method you feel you are comfortable with, you can use that method to do some particular task in the framework.

In this post, we will learn that how we can pass the data to views.

There are various ways of passing data to views.

  • Using view()
  • Using with()
  • Using compact()
  • Using withName()

Using view()

With the help of view() helper function we can directly pass the data to view from controller or routes.

Write the following code in the ‘web.php’ file

Route::get('/', function () {
    return view('welcome', ['PostName' => 'How to pass data to view in Laravel']);
});

In view(), the first argument is the name of the view, and the second is data that needs to pass to view. Data could be single or in the array.

Create and write the following code in the ‘welcome.blade.php’ file in ‘resources/views’ directory.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
</head>
<body>
    My Post Name
    {{ $PostName }}
</body>
</html>

Here we know that ‘{{ }}’ is being used for output the data.

We can pass an array with multiple values also.

Route::get('/', function () {
    return view('welcome', [ 'posts' =>  ['Post 1','Post 2','Post 3'] ]);
});

OR

Route::get('/', function () {
    $post = ['Post 1','Post 2','Post 3'];
    return view('welcome', [ 'posts' =>  $post ]);
});

Write the following code in the ‘welcome.blade.php’ file in ‘resources/views’ directory.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
</head>
<body>
    <h2>My Post Name</h2>

    @foreach ($posts as $post) 
       <li>{{ $post }}</li> 
    @endforeach 

</body>
</html>

When you pass the data in array, you can output the data with foreach loop.

Using with()

The ‘with()’ is a method to pass the data from controller routes to view.

Write the following code in the ‘web.php’ file.

Route::get('/', function () {
    return view('welcome')->with('post1','postdata')->with('post2','postdata2');
});

Write the following code in the ‘welcome.blade.php’ file in ‘resources/views’ directory.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
</head>
<body>
    <h2>My Post Name</h2>
    {{ $post1 }}
    {{ $post2 }}
   
</html>

It is basically used to pass flash data.

Using compact()

compact()’ is a PHP function. It is used for creating an array. the compact is most commonly used in Laravel to pass the data to view.

Write the following code in the ‘web.php’ file

Route::get('/', function () {
    $posts = ['post1','post2'];
    $postName = 'How to pass data to view in Laravel';
    return view('welcome',compact('posts','postName'));
});

Write the following code in the ‘welcome.blade.php’ file in ‘resources/views’ directory.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
</head>
<body>
    <h2>My Post Name</h2>
    {{ $postName }}
    <br>
    @foreach ($posts as $post) 
       <li>{{ $post }}</li> 
    @endforeach 
</body>
</html>

You can pass single and array value in compact.

Using withName()

withName is a Laravel magic method which is used to pass the flash message or single variable. You can use method chaining and send multiple data with different names like withName, withData, or anything after with.

Now when you have to access the data in view you simply need to print name attach with with. Like in when you pass the data using withData you can print it in view with {{ $data }} and when you pass the data using withPost you can print it with {{ $post }}

Route::get('/', function () {
    return view('welcome')->withName('postName')->withData('dataName');
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
</head>
<body>
    <h2>My Post Name</h2>
    {{ $name }}
    <br>
    {{ $data }}
  
</body>
</html>

You can also check What are the Route Groups and its use in Laravel

Spread the love