View in Laravel 7

Views are the HTML content served by the server. These are served by returning from controllers. As discussed in last tutorial, views are .php or .blade.php files which are stored in resources/views directory.

First view file:

Make a new file with name mywebpage.php in views folder.

  1. !DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>My first webpage</title>
  5. </head>
  6. <body>
  7. <h1>My Page</h1>
  8. <p>this is my page</p>
  9. </body>
  10. </html>

Now change your routes and controllers as:

  1. <?php
  2. // change routes.php to like this
  3. Route::get('request/{id?}','YourController@index');
  4. // in app >> Http >> Controllers >> YourController.php >> YourController
  5. public function index($id = 12)
  6. {
  7. return view('mywebpage');
  8. }

Now try these urls:

  • localhost:8000/request
  • http://localhost:8000/request/234
  • http://localhost:8000/request/33

NOTE: If view file is inside the directory then dot (.) will be used to navigate to the directory. For example, if 503.blade.php is in errors folder then, we will use view('errors.503') for rendering this file.

Sending variables in template files:

Template files may require some additional information. This information is sent through a controller. It can be done by sending one more array as a second argument to view() function or by using with() function. For example, we want to pass user id in a template:

  1. <?php
  2. // no change in routes.php
  3. // in class YourController
  4. public function index($id = 12)
  5. {
  6. return view('mywebpage',['id' => $id]);
  7. }
  8. // in mywebpage.php
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <title>My first webpage</title>
  13. </head>
  14. <body>
  15. <h1>My Page</h1>
  16. <p>this is my page</p>
  17. <p>my id is <?php echo $id?></p>
  18. </body>
  19. </html>

Now try above links again and see the result.

Note:

  1. In array (second argument of view()), key is the variable name which is required in template and value is the variable in controller. In other words, we are mapping a template required variable to a variable which is accessible in controller.

With function:

Variable/ data can be send using with() function. with() function also takes array and do the same work.

  1. return view('mywebpage')->with(['id'=>$id]);

Magic Methods:

There is one more way for passing data from controller to view file called Magic Methods. This way is used only when we have to pass single variable. A function is generated dynamically with prefix "with" and followed by capitalized variable name. It takes only one argument, value of variable. In our case, this function is withId($id).

  1. public function index($id)
  2. {
  3. return view('mywebpage')->withId($id);
  4. }

For general use function with+(required variable name with first capital letter)(). For example:

Variable NameMagic Method
namewithName()
my_namewithMy_name()
my_name_8withMy_name_8

NOTE: Magic methods are only work for variable with name in snack case (only lowercase letters, number and _). It will not work for the variables which contains capital letters like My_name or myName (but will work for my_name).

Further Reading:



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

With the expanding market of mobile apps, the developers are struggling to maintain the code bases for Native apps. M...
5 Elite and Imperative Hybrid App Frameworks
Django is a great framework in python. But all of the hosting do not provide django hosting in their shared or free h...
Cheap Django Hosting
Laravel provides blade template. Blade files are similar to php files and cover all features of php files. In additio...
Blade Template in Laravel 7

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Subscribe

Subscribe now and get weekly updates directly in your inbox!