{"id":658,"date":"2023-06-27T22:08:00","date_gmt":"2023-06-27T22:08:00","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=658"},"modified":"2023-06-26T10:14:01","modified_gmt":"2023-06-26T10:14:01","slug":"how-to-use-compact-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-to-use-compact-in-laravel\/","title":{"rendered":"How to use compact in laravel"},"content":{"rendered":"\n<p>In Laravel, the <code>compact<\/code> function is commonly used in controllers to pass variables to blade views. The <code>compact<\/code> function creates an array where the keys are variable names and the values are the corresponding variable values. Here&#8217;s how you can use <code>compact<\/code> in a Laravel controller to pass variables to a blade page:<\/p>\n\n\n\n<p>In your controller method, define the variables that you want to pass to the blade view:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function index()\r\n{\r\n    $name = 'John';\r\n    $age = 30;\r\n\r\n    return view('my-page', compact('name', 'age'));\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>In this example, we define the variables <code>$name<\/code> and <code>$age<\/code> with their corresponding values.<\/p>\n\n\n\n<p>Pass the variables to the blade view using the <code>compact<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return view('my-page', compact('name', 'age'));\r\n<\/code><\/pre>\n\n\n\n<p>The <code>compact('name', 'age')<\/code> statement creates an array with the keys <code>'name'<\/code> and <code>'age'<\/code> and their corresponding values from the variables.<\/p>\n\n\n\n<p>Access the variables in your blade view:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1>Welcome, {{ $name }}&lt;\/h1>\r\n&lt;p>Your age is: {{ $age }}&lt;\/p>\r\n<\/code><\/pre>\n\n\n\n<p>In the blade view, you can access the variables using the <code>$<\/code> sign followed by the variable name (<code>$name<\/code> and <code>$age<\/code> in this example).<\/p>\n\n\n\n<p>The <code>compact<\/code> function is a convenient way to pass variables to blade views without explicitly creating an associative array. It helps keep your code clean and readable.<\/p>\n\n\n\n<p>Alternatively, you can also use the <code>with<\/code> method when returning the view to pass variables individually:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return view('my-page')->with('name', $name)->with('age', $age);\r\n<\/code><\/pre>\n\n\n\n<p>In this case, each variable is passed individually using the <code>with<\/code> method.<\/p>\n\n\n\n<p>Both approaches achieve the same result of passing variables from the controller to the blade view. Choose the one that suits your coding style and preferences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Laravel, the compact function is commonly used in controllers to pass variables to blade views. The compact function creates an array where the keys are variable&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[42,35],"class_list":["post-658","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-laravel","tag-php"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":659,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/658\/revisions\/659"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}