Still on route topic, now we learn how to send data from router to view. It is so important, because sometimes we have data that can sometimes change. Come on, just check it out.
source code of previous article here
Throw Data from Route
1. Open the routes folder and open the file web.php
2. From what we learn previously , we have created 2 pages namely main page or / and page about or /.
Let's change our /about route with the code below
Route::get('/about', function () {
return view('about', array(
"webname" => "gepcode.com",
"owner" => "Gilang Pratama",
"usiaweb" => "7 Bulan"
));
});
Well, from the code above, we can see that we are passing an array to view 'about' Okay, next is how to receive data from route on our view.
Receive Data from Route In View
1. Open the file resources/view/about.blade.php as shown below
2. Then we just echo the key from the array we threw from our route . for example
<!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>Document</title>
</head>
<body>
<table>
<tbody>
<tr>
<td>Nama Web </td>
<td> : </td>
<td>
<?php echo $webname ?>
</td>
</tr>
<tr>
<td>Pemilik Web </td>
<td> : </td>
<td>
<?php echo $owner ?>
</td>
</tr>
<tr>
<td>Usia web </td>
<td> : </td>
<td>
<?php echo $usiaweb ?>
</td>
</tr>
</tbody>
</table>
</body>
</html>
After that, let's run with php artisan serve and open http://127.0.0.1:8000/about
then the result will be like below
Easy isn't it? good luck. for those looking for the source code there is here it is