MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to pass data to views in Laravel.

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.

Laravel controller Printdata function returning index view with data passed via array()

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.

Index.blade.php showing title variable and a Blade @foreach loop printing each name

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).

Laravel view using PHP print_r to print the title and name variables

ii. We use foreach function of PHP to print the data of variables to the view. See the below image.

Laravel view using a plain PHP foreach loop to echo each name with a line break

After calling the variable we set Route in web.php file, for routing see the below example.

Web.php route mapping the index URL to FirstController@Printdata, labeled address, controller, function

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:-

Browser at 127.0.0.1:8000/index showing Index page heading and names Sushant, Ajay, Amit

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:-

Laravel Printdata function returning index view with compact of name and title

There is no change in view file i.e., index.blade.php see the below image:-

Index.blade.php printing hello with title and looping over names using Blade @foreach

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:-

Browser at 127.0.0.1:8000/index showing Index page heading and names Sushant, Ajay, Amit

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:-

Laravel Printdata function passing name and title to the view using with() and an array

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:-

Browser at 127.0.0.1:8000/index showing Index page heading and names Sushant, Ajay, Amit

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:-

Laravel Printdata function passing data using withTitle and withName magic methods

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:-

Browser at 127.0.0.1:8000/index showing Index page heading and names Sushant, Ajay, Amit

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

Laravel breeze vs laravel fortify comparison

Here’s a comparison table between Laravel Breeze and Laravel Fortify to help you understand their differences and choose the right tool for your project: Feature Laravel Breeze…

Read More

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More

Laravel Tutorials: PHP Templating using Blade

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Complete PHP with Laravel Certification Guide & tutorials

What is PHP with Laravel? PHP was visioned eventually in the fall of 1994 by Rasmus Lerdorf. PHP means Hypertext Preprocessor. It’s a extensively- used, open source…

Read More

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-2

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Step 1. Create an index.blade.php file within resource/views/Country/ folder. Step 2.  Create a Controller within the…

Read More

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-1

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? How to Store Seed file into Database. Click There are two…

Read More