<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nick Boyle on Medium]]></title>
        <description><![CDATA[Stories by Nick Boyle on Medium]]></description>
        <link>https://medium.com/@bicknoyle?source=rss-4e1d742edbf0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GSJ2uPmr0ALnBKscGGMFUg.jpeg</url>
            <title>Stories by Nick Boyle on Medium</title>
            <link>https://medium.com/@bicknoyle?source=rss-4e1d742edbf0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:31:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bicknoyle/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My Laravel Tricks]]></title>
            <link>https://medium.com/@bicknoyle/my-laravel-tricks-57e2d0cc28c1?source=rss-4e1d742edbf0------2</link>
            <guid isPermaLink="false">https://medium.com/p/57e2d0cc28c1</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Nick Boyle]]></dc:creator>
            <pubDate>Thu, 03 Nov 2016 00:29:21 GMT</pubDate>
            <atom:updated>2016-11-03T00:29:21.422Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been considering moving <a href="http://www.bicknoyle.com/">my blog</a> to Medium for some time, but wanted to try out posting a few things here first. So let’s try it out, with a quick write-up on some of the “Tricks” or common things I do when working in Laravel. Feel free to holler back with some of your own!</p><h3>Mimicking “strong parameters” from Rails</h3><p>Rails has an interesting feature known as <a href="http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters">Strong Parameters</a>. They help make passing data to your models more secure by being explicit about the fields you want. The standard practice is to write a controller method to return the desired fields into your model’s constructor or update method. I do the same thing in Laravel:</p><pre>class PostController extends Controller {<br><br>    protected function postParams(Request $request)<br>    {<br>        return array_merge(<br>            $request-&gt;only([&#39;title&#39;, &#39;body&#39;]),<br>            [<br>                &#39;public&#39; =&gt; $request-&gt;input(&#39;publish_now&#39;, false),<br>            ]<br>        );<br>    }<br>}<br><br>// Creating new posts<br>$post = new Post($this-&gt;postParams($request));</pre><pre>// Updating existing posts<br>$post-&gt;update($this-&gt;postParams($request));</pre><p>This is also a great way to encapsulate any logic you might need to perform on a field before passing it into a model, such as parsing out a phone number or date.</p><h3>Creating related models</h3><p>Also inspired from Rails, I tend to use the create() method on a relationship instead of the new &amp; save() combo.</p><pre>// Instead of this...<br><br>// $comment = new Comment($input);<br>// $post-&gt;comments()-&gt;save($comment);<br><br>// ...do this...<br>$comment = $post-&gt;comments()-&gt;create($input);</pre><p>I’m not usually a subscriber to the “less code is better” mentality; I just find it more readable.</p><h3>Using class constants for “types”</h3><p>Say you have a Post model, and posts can have a “type” like story, media, quote, etc. Instead of defining types in a separate table and using a relation, I often define them as constants on the model, and just store the value in a column:</p><pre>class Post extends Model<br>{<br>    const TYPE_STORY = 0;<br>    const TYPE_MEDIA = 1;<br>    const TYPE_QUOTE = 2;<br>    <br>    // ...<br>}<br><br>// Usage<br>$post = new Post([&#39;type&#39; =&gt; Post::TYPE_STORY]);</pre><pre>if (Post::TYPE_MEDIA === $post-&gt;type) {<br>    // ...<br>}</pre><p>My litmus test for knowing when to do this (versus a relation) is as follows— If not having a particular type in the database would break my app, and the list of types will change very rarely or never, I use a class constant.</p><h3>Generating test data</h3><p>Factories are useful for seeding records in the DB, and also for creating test data:</p><pre>public function test_user_can_create_post()<br>{<br>    $post = factory(Post::class)-&gt;make();<br>    $this-&gt;visit(&#39;/posts/create&#39;)<br>         -&gt;type($post-&gt;title, &#39;title&#39;)<br>         -&gt;type($post-&gt;body, &#39;body&#39;)<br>         -&gt;press(&#39;Submit&#39;)<br>         -&gt;seeInDatabase(&#39;posts&#39;, [&#39;title&#39; =&gt; $post-&gt;title]);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57e2d0cc28c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>