Views in Laravel contain logic which determines how to present the final outcome of the web application to the users. The output is often the HTML output which, after each response, goes back to the users. The resources/views directory holds these views by default in Laravel.
You can use the view helper function to call the view in Laravel.
view(string $path, array $data = )
The path to the file becomes the first parameter in the above example whereas an optional array of data is the second parameter. You can use this array to pass data to the view.
Now, you would use the following function to call the resources/view/example.php.
Similarly, it is possible to call views within subfolders using dot notation. For example, you can call resources/views/project/parts/parts.php using the following command.
You can also include HTML and PHP together within a view file as shown in the following example.
<!DOCTYPE html> <html> <head> <title>Hello world!</title> </head> <body> <h1>Welcome!</h1> <p>Your name is: <?php echo $name; ?></p> </body> </html>
We are outputting the $name variable in the above example. We will actually pass an array of values at the time of calling the view helper class to pass this value to the view.
view('example', ['name' => $name]);
Alternatively, we can also use the compact() helper class. We will then pass a string to the compact() class which will become the variable name we are going to pass to the view.
Laravel also provides easy syntax for creating PHP control structures. All the control structures start with @[structure] whereas @[endstructure] proceeds each of these structures as well. Keep in mind that you will write the normal HTML inside the tags. Similarly, we will include a variable using Laravel syntax.
In the next lecture, we will study one of the most important types of control structures that are loops.