DEV Community

Cover image for 3 Ways to pass data to view in laravel 8
Yunwen Eric
Yunwen Eric

Posted on

3 Ways to pass data to view in laravel 8

Introduction

Laravel is a MVC(Model View Controller) Framework build with PHP. It has been one of the most starred and most popular backend framework on github. This is simply because Laravel makes it easy for developers to build small, medium or large|complex applications in a very short time with little stress and fewer lines of code.

In Laravel, our view is written using the blade templating engine, which is pretty nice and quiet easy to learn as well.

For this tutorial, we are going to use a fresh laravel Project

1. Create a laravel project with composer or laravel installer
laravel new Dataparse
Enter fullscreen mode Exit fullscreen mode
2. Create your routes in the web.php file
Route::get('/users', 
[ProductController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode
Route::get('/users', 
[ProductController::class, 'usingwith']);
Enter fullscreen mode Exit fullscreen mode
Route::get('/users', 
[ProductController::class, 'usingview']);
Enter fullscreen mode Exit fullscreen mode
3. Create your controller using the php artisan command
php artisan make:controller ProductController
Enter fullscreen mode Exit fullscreen mode

This creates the Product controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    \\
}

Enter fullscreen mode Exit fullscreen mode
Create view in the resources/views folder

Here create a file called home.blade.php

Some of the ways to pass data to the view dynamically are;

1. Using the compact Method:

The compact method creates an array from existing variables given as string arguments to it. Below is an example of how we'd use the compact function.

 public function index(){
        $name = "Tony Stack";
        return view('home', compact('name'));
    }
Enter fullscreen mode Exit fullscreen mode

In view, use the blade syntax to parse data

<!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>User</title>
</head>
<body>
    <h1>User Page</h1>
    <p>{{ $name }}</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

2. Using the With Method:

The with method allows you to pass just a single variable to a view.

 public function usingwith(){
        $name = "Tony Stack";
        return view('home')->with('name', $name);
    }
Enter fullscreen mode Exit fullscreen mode

In the home.blade.php file parse the date

<!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>User</title>
</head>
<body>
    <h1>User Page</h1>
    <p>{{ $name }}</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

3. Passing data directly in view method:

 public function usingview(){
        $data = [
            "name"=>"John Doe",
            "age" => "23"

        ];
        return view('home', ["data"=>$data]);
    }
Enter fullscreen mode Exit fullscreen mode

In the view, parse the data using the @foreach blade syntax

<!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>User</title>
</head>
<body>
    <h1>User Page</h1>
    @foreach ($data as $item )
    {{ $item }}
    @endforeach
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Hurray you made it. Congratulations!

Top comments (0)