{"id":9387,"date":"2024-01-24T05:09:59","date_gmt":"2024-01-24T05:09:59","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9387"},"modified":"2024-07-04T08:23:59","modified_gmt":"2024-07-04T08:23:59","slug":"with-and-compact-functions-in-laravel","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/laravel\/with-and-compact-functions-in-laravel","title":{"rendered":"Exploring the with() and compact() Functions in Laravel"},"content":{"rendered":"<p>Laravel offers a wide range of functionalities and components that can be leveraged to create efficient and scalable web applications. Two such functions that are essential for passing data from the controller to the view are `with()` and `compact()`.<\/p>\n<p>We will explore the `with()` and `compact()` functions in depth, gaining an understanding of their purpose and mastering their effective utilization within Laravel.<\/p>\n<h3>The `with()` Method:<\/h3>\n<p>The `with()` method allows us to pass variables to the blade file that will be displayed in the corresponding view. The `with()` method takes two parameters: the name of the variable and its value. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">public function index() \r\n{\r\n$data = [\r\n'title' =&gt; 'Users List',\r\n'users' =&gt; Users::all(),\r\n];\r\n\r\nreturn view('users.index')-&gt;with($data);\r\n}\r\n\r\n<\/pre>\n<p>In the view file, you can directly access these variables. Let\u2019s see view file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n<!-- users\/index.blade.php -->\r\n<h1>{{ $title }}<\/h1>\r\n@foreach ($users as $user)\r\n<p>{{ $user->name }}<\/p>\r\n@endforeach\r\n<\/pre>\n<h3>The `compact()` Method:<\/h3>\n<p>`compact()` is a PHP function. It creates an array containing variables and their values. Let\u2019s assume you have the following variables.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n$name = 'Martin'; \r\n$organization = 'Bacancy'; \r\n$technology = 'Laravel';\r\n<\/pre>\n<p>Put those variables in a `compact()`:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n$data = compact(['name', 'organization', 'technology']);\r\n<\/pre>\n<p>We\u2019&#8217;ll get following array assigned to `$data`:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n[ \r\n    'name' => 'Martin', \r\n    'organization' => 'Bacancy', \r\n    'technology' => 'Laravel' \r\n]\r\n<\/pre>\n<p>The `compact()` method in Laravel allows us to pass variables to view without explicitly specifying their names. This simplifies the process of passing multiple variables to the view by generating an associative array of variable names and their values. For instance:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\npublic function index() \r\n{\r\n$title = 'Users List';\r\n$users = Users::all();\r\n\r\nreturn view('users.index', compact('title', 'users'));\r\n}\r\n<\/pre>\n<p>And the view file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n<!-- users\/index.blade.php -->\r\n<h1>{{ $title }}<\/h1>\r\n\r\n@foreach ($users as $user)\r\n<p>{{ $user->name }}<\/p>\r\n@endforeach\r\n<\/pre>\n<h3>When to Use Which:<\/h3>\n<p>Both the `with()` and `compact()` methods play pivotal roles in Laravel applications, `with()` is more explicit when we need to pass an array with meaningful keys, on the other hand `compact()` is concise and suitable for simpler scenarios.<\/p>\n<p>Understanding the nuances of when to use `with()` and `compact()` in Laravel enables developers to make informed decisions about how to pass data from the controller to the view, leading to more effective, maintainable, and organized code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel offers a wide range of functionalities and components that can be leveraged to create efficient and scalable web applications. Two such functions that are essential for passing data from the controller to the view are `with()` and `compact()`. We will explore the `with()` and `compact()` functions in depth, gaining an understanding of their purpose [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9388,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-9387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9387"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9387"}],"version-history":[{"count":4,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9387\/revisions"}],"predecessor-version":[{"id":9392,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9387\/revisions\/9392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9388"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}