{"id":11193,"date":"2020-02-26T12:36:18","date_gmt":"2020-02-26T12:36:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=11193"},"modified":"2021-11-12T06:50:12","modified_gmt":"2021-11-12T06:50:12","slug":"how-to-pass-data-to-views-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-pass-data-to-views-in-laravel\/","title":{"rendered":"How to pass data to views in Laravel."},"content":{"rendered":"\n<h2 class=\"has-text-align-center wp-block-heading\">There are 4 ways to send data to view:- <\/h2>\n\n\n\n<p><em>1. Using an array function : array()<br>2. Using compact function : compact()<br>3. Using with : with([]) <br>4. Using withvariablename : withName()<\/em><\/p>\n\n\n\n<p><strong><em>1. Using an array function:-<\/em><\/strong>  First, we create a function and define variables in it and use array function to send data in views.  See the below image.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"134\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Creating-function.png\" alt=\"\" class=\"wp-image-11204\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Creating-function.png 455w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Creating-function-300x88.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/figure><\/div>\n\n\n\n<p>in the above function, we created two variables:- <strong><em>$name<\/em><\/strong> and <em><strong>$title<\/strong><\/em> 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. <\/p>\n\n\n\n<p>After that, we make a page name <em><strong>index.blade.php<\/strong><\/em> 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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"216\" height=\"171\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/index-file.png\" alt=\"\" class=\"wp-image-11208\"\/><\/figure><\/div>\n\n\n\n<p>In the above page, foreach method of Laravel is used to print the value of variables. If you don&#8217;t know about for each method of laravel so don&#8217;t worry, see the below example in which we use the PHP function to receive the value of variables.<\/p>\n\n\n\n<p>i. In the below example, we used <strong><em>print_r()<\/em><\/strong> function of <em>PHP<\/em> to print the data of variables to the view. See the below image (the variable passed under the <em><strong>print_r<\/strong><\/em> function is the <strong><em>key name<\/em><\/strong> passed in the array in the controller).  <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"171\" height=\"158\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/php-function-in-view.png\" alt=\"\" class=\"wp-image-11218\"\/><\/figure><\/div>\n\n\n\n<p>ii. We use <strong><em>foreach<\/em><\/strong> function of PHP to print the data of variables to the view. See the below image.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"238\" height=\"152\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/PHP-function-in-view-2.png\" alt=\"\" class=\"wp-image-11233\"\/><\/figure><\/div>\n\n\n\n<p>After calling the variable we set  <em><strong>Route<\/strong><\/em> in <em><strong>web.php<\/strong><\/em> file, for routing see the below example. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"346\" height=\"133\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/route-in-php.png\" alt=\"\" class=\"wp-image-11239\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/route-in-php.png 346w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/route-in-php-300x115.png 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/figure><\/div>\n\n\n\n<p>Now, we run the Server with the command <strong><em>php artisan serve<\/em><\/strong> and copy the address and add the index after that. See the output below:-<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"313\" height=\"167\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png\" alt=\"\" class=\"wp-image-11244\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png 313w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array-300x160.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/figure><\/div>\n\n\n\n<p> <em><strong>2. Using compact function:-<\/strong><\/em>  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:-<\/p>\n\n\n\n<p>We have to call the <strong><em>compact()<\/em><\/strong> function where we called the <strong><em>array()<\/em><\/strong> funtion. See the below image:-<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"361\" height=\"108\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/compact-call.png\" alt=\"\" class=\"wp-image-11251\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/compact-call.png 361w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/compact-call-300x90.png 300w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/figure><\/div>\n\n\n\n<p>There is no change in view file i.e., index.blade.php see the below image:-<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"209\" height=\"151\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Compact-index.png\" alt=\"\" class=\"wp-image-11253\"\/><\/figure><\/div>\n\n\n\n<p>Also no changes in web.php file which is in <em><strong>Route<\/strong><\/em> folder.<\/p>\n\n\n\n<p> Now, we run the Server with the command <strong><em>php artisan serve<\/em><\/strong> and copy the address and add the index after that. See the output below:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"313\" height=\"167\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png\" alt=\"\" class=\"wp-image-11244\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png 313w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array-300x160.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/figure><\/div>\n\n\n\n<p> <em><strong>3. Using with:-<\/strong><\/em>  As the compact(), no need to change the route and view, only change the function in the controller. See the below image:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"457\" height=\"119\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/with-controller.png\" alt=\"\" class=\"wp-image-11261\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/with-controller.png 457w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/with-controller-300x78.png 300w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/figure><\/div>\n\n\n\n<p>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:-<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"313\" height=\"167\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png\" alt=\"\" class=\"wp-image-11244\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png 313w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array-300x160.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/figure><\/div>\n\n\n\n<p> <em><strong>4. Using withvariablename:-<\/strong><\/em>  As the above function&#8217;s,  no need to change the route and view, only change the function in the controller. See the below image:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"447\" height=\"123\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/withname-controller.png\" alt=\"\" class=\"wp-image-11274\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/withname-controller.png 447w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/withname-controller-300x83.png 300w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/figure><\/div>\n\n\n\n<p> 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:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"313\" height=\"167\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png\" alt=\"\" class=\"wp-image-11244\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array.png 313w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Output-of-array-300x160.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/figure><\/div>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_13236\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_17118\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_13236\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":19,"featured_media":11483,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5201],"tags":[],"class_list":["post-11193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=11193"}],"version-history":[{"count":53,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11193\/revisions"}],"predecessor-version":[{"id":24907,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11193\/revisions\/24907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/11483"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=11193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=11193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=11193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}