{"id":190,"date":"2014-03-03T23:17:41","date_gmt":"2014-03-03T23:17:41","guid":{"rendered":"https:\/\/www.joetannorella.com\/?p=190"},"modified":"2014-11-24T18:26:21","modified_gmt":"2014-11-24T18:26:21","slug":"how-to-add-an-attribute-from-a-model-query-in-laravel","status":"publish","type":"post","link":"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/","title":{"rendered":"How to add an attribute from a model query in laravel"},"content":{"rendered":"

So I’m still very much into learning Laravel. One project I’m building at the moment is\u00a0imrepo.com<\/a>, which when you strip it down to the basics, is basically just a directory of ‘things’ – not the most complex project ever, but I’m still finding that I’m learning a lot along the way.<\/p>\n

I got around to releasing a beta version of the website and then looked back at my codebase. It was a mess. I’m very much an advocate of “if it’s not broke, don’t try to fix it”, especially when it comes to launching new projects (\u00e0 la the Lean Startup philosophy). However, one of my goals for creating this project is to learn Laravel to a high level. I want to know where I’m swaying from Laravel best practices and how to correct it, as well as learn anything to make me a more efficient developer along the way.<\/p>\n

The issue here was that I was adding custom attributes to my model query results from my controllers. To make matters worse, I kept repeating code in the process. I created a function and put it into my \/app\/require\/functions.php file, and it worked fine. But then I realised that this solution is far from ideal.<\/p>\n

So here is the evolution of how I fixed this issue (sorry about the pseudo code, hopefully you get the idea):<\/p>\n

Original (poor code!):<\/h3>\n
\r\nHomeController {\r\n    foreach ($posts and $post) {\r\n       <generate URL based on column values>\r\n    }\r\n}\r\nPostsController {\r\n    foreach ($posts and $post) {\r\n        <generate URL based on column values>\r\n    }\r\n}\r\n<\/pre>\n

Slightly improved (DRY principle)<\/h3>\n
\r\nHomeController {\r\n    $posts = make_post_variables($posts);\r\n}\r\nPostsController {\r\n    $posts = make_post_variables($posts);\r\n}\r\nFile: \/app\/require\/functions.php\r\npublic function make_post_variables($postsarray) {\r\n    foreach ($posts and $post) {\r\n        <generate URL based on column values>\r\n    }\r\n}\r\n<\/pre>\n

Optimal (using the model)<\/h3>\n
\r\nHomeController {\r\n    $posts = Post::orderBy('created_at', 'DESC')->get();\r\n}\r\nPostsController {\r\n    $posts = Post::orderBy('created_at', 'DESC')->get();\r\n}\r\n<\/pre>\n

File: \/app\/model\/Post.php<\/strong><\/p>\n

\r\nprotected $appends = array('Url');\r\npublic function getUrlAttribute() {\r\n    $url = "\/product\/" . $this->id . "\/" . sluggify($this->post_title);\r\n    return $url;\r\n}\r\n<\/pre>\n

What this final way of doing it means is that every time my Post model gets queried, essentially a new column is being created called “url” which mimics a real column from the database.<\/p>\n

Sorry about the poor formatting on this blog so far. It’s on my to-do list of things to fix, I promise!<\/p>\n

Feel free to ask any questions in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"

So I’m still very much into learning Laravel. One project I’m building at the moment is\u00a0imrepo.com, which when you strip it down to the basics, is basically just a directory of ‘things’ – not the most complex project ever, but I’m still finding that I’m learning a lot along the way. I got around to…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[12],"yoast_head":"\nHow to add an attribute from a model query in laravel - JoeTannorella.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add an attribute from a model query in laravel - JoeTannorella.com\" \/>\n<meta property=\"og:description\" content=\"So I’m still very much into learning Laravel. One project I’m building at the moment is\u00a0imrepo.com, which when you strip it down to the basics, is basically just a directory of ‘things’ – not the most complex project ever, but I’m still finding that I’m learning a lot along the way. I got around to...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"JoeTannorella.com\" \/>\n<meta property=\"article:published_time\" content=\"2014-03-03T23:17:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-24T18:26:21+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/\",\"url\":\"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/\",\"name\":\"How to add an attribute from a model query in laravel - JoeTannorella.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.joetannorella.com\/#website\"},\"datePublished\":\"2014-03-03T23:17:41+00:00\",\"dateModified\":\"2014-11-24T18:26:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.joetannorella.com\/#website\",\"url\":\"https:\/\/www.joetannorella.com\/\",\"name\":\"JoeTannorella.com\",\"description\":\"Web Development | Digital Marketing | Business\/Startups | Analytics\/data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.joetannorella.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.joetannorella.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add an attribute from a model query in laravel - JoeTannorella.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"How to add an attribute from a model query in laravel - JoeTannorella.com","og_description":"So I’m still very much into learning Laravel. One project I’m building at the moment is\u00a0imrepo.com, which when you strip it down to the basics, is basically just a directory of ‘things’ – not the most complex project ever, but I’m still finding that I’m learning a lot along the way. I got around to...","og_url":"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/","og_site_name":"JoeTannorella.com","article_published_time":"2014-03-03T23:17:41+00:00","article_modified_time":"2014-11-24T18:26:21+00:00","author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/","url":"https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/","name":"How to add an attribute from a model query in laravel - JoeTannorella.com","isPartOf":{"@id":"https:\/\/www.joetannorella.com\/#website"},"datePublished":"2014-03-03T23:17:41+00:00","dateModified":"2014-11-24T18:26:21+00:00","author":{"@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.joetannorella.com\/how-to-add-an-attribute-from-a-model-query-in-laravel\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.joetannorella.com\/#website","url":"https:\/\/www.joetannorella.com\/","name":"JoeTannorella.com","description":"Web Development | Digital Marketing | Business\/Startups | Analytics\/data","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.joetannorella.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.joetannorella.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/190"}],"collection":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":10,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/190\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}