{"id":32,"date":"2024-01-21T17:09:55","date_gmt":"2024-01-21T17:09:55","guid":{"rendered":"https:\/\/czerniejewski.pl\/?p=32"},"modified":"2024-01-22T20:25:30","modified_gmt":"2024-01-22T20:25:30","slug":"relationship-methods-vs-dynamic-properties","status":"publish","type":"post","link":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/","title":{"rendered":"Relationship Methods vs. Dynamic Properties"},"content":{"rendered":"\n<p>Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy \ud83d\ude42<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\User;\n \n$user = User::find(1);\n \n#Relationship Methods (RM)\nforeach ($user->posts()->get() as $post) {\n    \/\/ ...\n}\n\nvs\n\n#Dynamic Properties (DP)\nforeach ($user->posts as $post) {\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>Pierwszy spos\u00f3b (RM) ma wiele wad. Pierwsz\u0105 z nich jest lazy loading. Czyli N+1 query problem i je\u017celi u\u017cyjemy  <code>$user->posts()->get()<\/code> 2 razy w ci\u0105gu jednego request&#8217;u, zostan\u0105\u00a0zrobione 2 takie same zapytanie do bazy danych.<\/p>\n\n\n\n<p>Drugi spos\u00f3b (DP). U\u017cywa <code>Eager Loading<\/code>, wi\u0119c problem N+1 i powtarzaj\u0105cych si\u0119\u00a0zapyta\u0144 zostaje rozwi\u0105zany. Czy jest on idealny? Oczywi\u015bcie nie. <br>Laravel zapisuje dane z bazy danych w atrybucie statycznym. Statyczne atrybutu s\u0105 zapisane do klasy nie obiektu. <br><br>Pierwszy problem, ka\u017cde nowe stworzenie obiektu ju\u017c b\u0119dzie posiada\u0107 te dane. <br><br>Drugi problem je\u017celi dla walidacji u\u017cyjemy (DP). I zwr\u00f3cimy model bez \u017cadnych limit\u00f3w w kontrolerze (np. API resources), statyczne atrybuty zostan\u0105\u00a0zwr\u00f3cone do API.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User Controller extends Controller \n{\n  public function addEmail(User $user, string $email): array\n  {\n    if (in_array($email, $user->emails, true) {\n      throw new Exception('Email already assigned');\n    }\n\n    return $user; \/\/do api zostanie dodany atrybut emails, mimo \u017ce to nie by\u0142o naszym celem.\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy \ud83d\ude42 Pierwszy spos\u00f3b (RM) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[9,4],"tags":[3],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Relationship Methods vs. Dynamic Properties - Marian Czerniejewski<\/title>\n<meta name=\"description\" content=\"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\" \/>\n<meta property=\"og:locale\" content=\"pl_PL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Relationship Methods vs. Dynamic Properties - Marian Czerniejewski\" \/>\n<meta property=\"og:description\" content=\"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\" \/>\n<meta property=\"og:site_name\" content=\"Marian Czerniejewski\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-21T17:09:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T20:25:30+00:00\" \/>\n<meta name=\"author\" content=\"Marian Czerniejewski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Napisane przez\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marian Czerniejewski\" \/>\n\t<meta name=\"twitter:label2\" content=\"Szacowany czas czytania\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minuta\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\"},\"author\":{\"name\":\"Marian Czerniejewski\",\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4\"},\"headline\":\"Relationship Methods vs. Dynamic Properties\",\"datePublished\":\"2024-01-21T17:09:55+00:00\",\"dateModified\":\"2024-01-22T20:25:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\"},\"wordCount\":147,\"publisher\":{\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4\"},\"keywords\":[\"laravel\"],\"articleSection\":[\"laravel\",\"tips\"],\"inLanguage\":\"pl-PL\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\",\"url\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\",\"name\":\"Relationship Methods vs. Dynamic Properties - Marian Czerniejewski\",\"isPartOf\":{\"@id\":\"https:\/\/czerniejewski.pl\/#website\"},\"datePublished\":\"2024-01-21T17:09:55+00:00\",\"dateModified\":\"2024-01-22T20:25:30+00:00\",\"description\":\"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)\",\"breadcrumb\":{\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#breadcrumb\"},\"inLanguage\":\"pl-PL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\/\/czerniejewski.pl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Relationship Methods vs. Dynamic Properties\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/czerniejewski.pl\/#website\",\"url\":\"https:\/\/czerniejewski.pl\/\",\"name\":\"Marian Czerniejewski\",\"description\":\"IT lifestyle\",\"publisher\":{\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/czerniejewski.pl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pl-PL\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4\",\"name\":\"Marian Czerniejewski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pl-PL\",\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/czerniejewski.pl\/wp-content\/uploads\/2023\/08\/headimage.jpg\",\"contentUrl\":\"https:\/\/czerniejewski.pl\/wp-content\/uploads\/2023\/08\/headimage.jpg\",\"width\":1896,\"height\":1794,\"caption\":\"Marian Czerniejewski\"},\"logo\":{\"@id\":\"https:\/\/czerniejewski.pl\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/czerniejewski.pl\"],\"url\":\"https:\/\/czerniejewski.pl\/index.php\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Relationship Methods vs. Dynamic Properties - Marian Czerniejewski","description":"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)","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:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/","og_locale":"pl_PL","og_type":"article","og_title":"Relationship Methods vs. Dynamic Properties - Marian Czerniejewski","og_description":"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)","og_url":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/","og_site_name":"Marian Czerniejewski","article_published_time":"2024-01-21T17:09:55+00:00","article_modified_time":"2024-01-22T20:25:30+00:00","author":"Marian Czerniejewski","twitter_card":"summary_large_image","twitter_misc":{"Napisane przez":"Marian Czerniejewski","Szacowany czas czytania":"1 minuta"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#article","isPartOf":{"@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/"},"author":{"name":"Marian Czerniejewski","@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4"},"headline":"Relationship Methods vs. Dynamic Properties","datePublished":"2024-01-21T17:09:55+00:00","dateModified":"2024-01-22T20:25:30+00:00","mainEntityOfPage":{"@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/"},"wordCount":147,"publisher":{"@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4"},"keywords":["laravel"],"articleSection":["laravel","tips"],"inLanguage":"pl-PL"},{"@type":"WebPage","@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/","url":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/","name":"Relationship Methods vs. Dynamic Properties - Marian Czerniejewski","isPartOf":{"@id":"https:\/\/czerniejewski.pl\/#website"},"datePublished":"2024-01-21T17:09:55+00:00","dateModified":"2024-01-22T20:25:30+00:00","description":"Jakie s\u0105 r\u00f3\u017cnic\u0119 mi\u0119dzy metod\u0105 a dynamicznym atrybutem. Kt\u00f3ry jest lepszy? Oczywi\u015bcie jak zawsze to zale\u017cy :)","breadcrumb":{"@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#breadcrumb"},"inLanguage":"pl-PL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/czerniejewski.pl\/index.php\/2024\/01\/21\/relationship-methods-vs-dynamic-properties\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/czerniejewski.pl\/"},{"@type":"ListItem","position":2,"name":"Relationship Methods vs. Dynamic Properties"}]},{"@type":"WebSite","@id":"https:\/\/czerniejewski.pl\/#website","url":"https:\/\/czerniejewski.pl\/","name":"Marian Czerniejewski","description":"IT lifestyle","publisher":{"@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/czerniejewski.pl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pl-PL"},{"@type":["Person","Organization"],"@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/8baf9933db16cac952b5971f540507c4","name":"Marian Czerniejewski","image":{"@type":"ImageObject","inLanguage":"pl-PL","@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/image\/","url":"https:\/\/czerniejewski.pl\/wp-content\/uploads\/2023\/08\/headimage.jpg","contentUrl":"https:\/\/czerniejewski.pl\/wp-content\/uploads\/2023\/08\/headimage.jpg","width":1896,"height":1794,"caption":"Marian Czerniejewski"},"logo":{"@id":"https:\/\/czerniejewski.pl\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/czerniejewski.pl"],"url":"https:\/\/czerniejewski.pl\/index.php\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/posts\/32"}],"collection":[{"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":2,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions\/35"}],"wp:attachment":[{"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/czerniejewski.pl\/index.php\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}