There are 4 ways to send data to view:-
1. Using an array function : array()
2. Using compact function : compact()
3. Using with : with([])
4. Using withvariablename : withName()
1. Using an array function:- First, we create a function and define variables in it and use array function to send data in views. See the below image.
in the above function, we created two variables:- $name and $title and return it to views. Now we call array() in which we make a key and assign the value of the key as the variable.
After that, we make a page name index.blade.php which is in the view and call our variables in it so that we receive the value from our controller. See the below image of the index page.
In the above page, foreach method of Laravel is used to print the value of variables. If you don’t know about for each method of laravel so don’t worry, see the below example in which we use the PHP function to receive the value of variables.
i. In the below example, we used print_r() function of PHP to print the data of variables to the view. See the below image (the variable passed under the print_r function is the key name passed in the array in the controller).
ii. We use foreach function of PHP to print the data of variables to the view. See the below image.
After calling the variable we set Route in web.php file, for routing see the below example.
Now, we run the Server with the command php artisan serve and copy the address and add the index after that. See the output below:-
2. Using compact function:- We use the same functions, route, and view(index.blade.php) which we used in the array() but we have to make changes in function. What changes, see below:-
We have to call the compact() function where we called the array() funtion. See the below image:-
There is no change in view file i.e., index.blade.php see the below image:-
Also no changes in web.php file which is in Route folder.
Now, we run the Server with the command php artisan serve and copy the address and add the index after that. See the output below:-
3. Using with:- As the compact(), no need to change the route and view, only change the function in the controller. See the below image:-
View and route remain the same as above, so we need to run the server(if not running) by the command php artisan serve and copy the address and add the index after that. See the output below:-
4. Using withvariablename:- As the above function’s, no need to change the route and view, only change the function in the controller. See the below image:-
View and route remain the same as above, so we need to run the server(if not running) by the command php artisan serve and copy the address and add the index after that. See the output below:-
- What is On-Page Optimization and Off-page Optimization - March 14, 2024
- [SOLVED] Flutter : PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null) - December 7, 2021
- [Solved] Flutter : Error: The getter ‘subhead’ isn’t defined for the class ‘TextTheme’ from package:flutter/src/material/text_theme.dart’ – searchable_dropdown - December 6, 2021