<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nour-Eddine ECH-CHEBABY</title>
    <description>The latest articles on DEV Community by Nour-Eddine ECH-CHEBABY (@chebaby).</description>
    <link>https://dev.to/chebaby</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F540444%2F52426edf-94f6-40be-b2ff-e972bc1145a8.png</url>
      <title>DEV Community: Nour-Eddine ECH-CHEBABY</title>
      <link>https://dev.to/chebaby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chebaby"/>
    <language>en</language>
    <item>
      <title>Laravel - validate 'true' or 'false' as boolean</title>
      <dc:creator>Nour-Eddine ECH-CHEBABY</dc:creator>
      <pubDate>Tue, 18 Jan 2022 22:21:02 +0000</pubDate>
      <link>https://dev.to/chebaby/laravel-validate-true-or-false-as-boolean-2gio</link>
      <guid>https://dev.to/chebaby/laravel-validate-true-or-false-as-boolean-2gio</guid>
      <description>&lt;p&gt;This &lt;a href="https://echebaby.com/blog/2021-12-30-laravel-validate-true-and-false-as-booleans/"&gt;post&lt;/a&gt; was originally published on my personal &lt;a href="https://echebaby.com/blog/"&gt;blog&lt;/a&gt; on 30th December 2021. &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Every once in a while you come across a situation where you need to validate a request input as a boolean, and the input value is &lt;code&gt;'true'&lt;/code&gt; or &lt;code&gt;'false'&lt;/code&gt; (notice that I wrapped the values inside single quotes to indicate that those are actually strings). You would expect, &lt;em&gt;as I did&lt;/em&gt;, that this will just work out of the box and Laravel validator will just treat them as booleans. But, that's not the case, instead, you will be hit with this beautiful error message &lt;code&gt;'The inputName field must be true or false.'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To deeply understand why this is happening, let's go through an example, let's say that we have a form request that we call &lt;code&gt;PostRequest&lt;/code&gt;. &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Form requests are custom request classes that encapsulate their own validation and authorization logic. &lt;br&gt;&lt;br&gt;-- Laravel docs.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Http\Requests&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Foundation\Http\FormRequest&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostRequest&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FormRequest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;authorize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'is_published'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required|boolean'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PostRequest&lt;/code&gt;, for simplicity purposes, has a single request input that, first, is &lt;strong&gt;required&lt;/strong&gt;, and second, is &lt;strong&gt;boolean&lt;/strong&gt;, and this input is named &lt;code&gt;is_published&lt;/code&gt;. Let's also say that &lt;code&gt;is_published&lt;/code&gt; value coming to us from the client is &lt;code&gt;'true'&lt;/code&gt; or &lt;code&gt;'false'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At this point we already know that validation wouldn't pass. So, how can we handle this? &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Beforehand
&lt;/h2&gt;

&lt;p&gt;Before diving in, I want to show you the logic Laravel uses to validate booleans internally, you can actually see that in &lt;code&gt;validateBoolean()&lt;/code&gt; method located in &lt;code&gt;Illuminate/Validation/Concerns/ValidatesAttributes&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Validation\Concerns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Validate that an attribute is a boolean.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;validateBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$acceptable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;in_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$acceptable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also see that in this unit tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Validation\Validator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;testValidateBoolean&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$trans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getIlluminateArrayTranslator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="nv"&gt;$v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Validator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$trans&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'false'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Boolean'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;passes&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="nv"&gt;$v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Validator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$trans&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Boolean'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;passes&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;sup&gt;Source : &lt;a href="https://github.com/laravel/framework/blob/8.x/tests/Validation/ValidationValidatorTest.php"&gt;https://github.com/laravel/framework/blob/8.x/tests/Validation/ValidationValidatorTest.php&lt;/a&gt;&lt;/sup&gt; &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The first approach - use prepareForValidation method &lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Let's start with what I think it's a more easy approach to implement, it's what Laravel calls "Preparing Input For Validation", &lt;br&gt;
and that's done by using &lt;code&gt;prepareForValidation()&lt;/code&gt; method. Like its name reveals, this method allows us to add new request inputs or update existing request inputs before going through the validation rules.&lt;/p&gt;

&lt;p&gt;So in our small example here, we will try to convert the &lt;code&gt;is_published&lt;/code&gt; value to an actual boolean, and merge it back to the original request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Http\Requests&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Foundation\Http\FormRequest&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostRequest&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FormRequest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'is_published'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required|boolean'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Prepare inputs for validation.
     *
     * @return void
     */&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;prepareForValidation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
            &lt;span class="s1"&gt;'is_published'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;is_published&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Convert to boolean
     *
     * @param $booleable
     * @return boolean
     */&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$booleable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;filter_var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$booleable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_BOOLEAN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_NULL_ON_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;FILTER_VALIDATE_BOOLEAN&lt;/code&gt; tries to be smart, recognizing words like &lt;em&gt;&lt;code&gt;'Yes'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'No'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'Off'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'On'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'true'&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;'false'&lt;/code&gt;&lt;/em&gt;, and is not case-sensitive when validating strings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FILTER_VALIDATE_BOOLEAN&lt;/code&gt; returns &lt;strong&gt;true&lt;/strong&gt; for &lt;em&gt;&lt;code&gt;'1'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'true'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'on'&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;'yes'&lt;/code&gt;&lt;/em&gt;. Returns &lt;strong&gt;false&lt;/strong&gt; otherwise.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;FILTER_NULL_ON_FAILURE&lt;/code&gt; flag is set, &lt;strong&gt;false&lt;/strong&gt; is returned ONLY for &lt;em&gt;&lt;code&gt;'0'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'false'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'off'&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;'no'&lt;/code&gt;&lt;/em&gt;, and &lt;em&gt;&lt;code&gt;''&lt;/code&gt;&lt;/em&gt;, and &lt;strong&gt;null&lt;/strong&gt; is returned for all non-boolean values.&lt;/p&gt;

&lt;p&gt;Understanding how &lt;code&gt;FILTER_NULL_ON_FAILURE&lt;/code&gt; flag affect the &lt;code&gt;filter_var&lt;/code&gt; function is essential, especially while tackling the second approach as we are going to see later on. &lt;br&gt;&lt;br&gt;
For that reason let me provide you with some examples to demonstrate how the &lt;code&gt;toBoolean&lt;/code&gt; method behave under different use cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;             &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'on'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;               &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'yes'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;              &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'false'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;            &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'off'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;              &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'no'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;               &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'not a boolean'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until here it makes perfect sense, &lt;em&gt;"truthy"&lt;/em&gt; booleans are &lt;code&gt;true&lt;/code&gt;, &lt;em&gt;"falsy"&lt;/em&gt; booleans are &lt;code&gt;false&lt;/code&gt;, others are just &lt;code&gt;null&lt;/code&gt;. Perfect!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBoolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This, is where it gets interested, this last use case could really be confusing, I myself was waiting for &lt;code&gt;null&lt;/code&gt; as a return value, but we get a &lt;code&gt;boolean&lt;/code&gt; instead &lt;sup&gt;&lt;em&gt;(&lt;code&gt;false&lt;/code&gt; in this case)&lt;/em&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;This will cause a &lt;strong&gt;false validation&lt;/strong&gt;, because the empty string will be valuated as a &lt;code&gt;boolean&lt;/code&gt;, which make the validation passes.&lt;/p&gt;

&lt;p&gt;Notice that this will never be the case in our example, because we have a &lt;code&gt;required&lt;/code&gt; rule, if the request input &lt;sup&gt;&lt;em&gt;(&lt;code&gt;is_published&lt;/code&gt;)&lt;/em&gt;&lt;/sup&gt; is an empty string, the validation will fail before even hitting the &lt;code&gt;boolean&lt;/code&gt; rule.&lt;/p&gt;

&lt;p&gt;I thought it was important to bring this up.&lt;/p&gt;

&lt;p&gt;With that been said, let's jump right into the second approach. &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The second approach - use a custom validation rule &lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;While the first approach works perfectly, there is a "classy" way to validate the input as boolean, and that's by creating custom validation rules using rule objects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Laravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the make:rule Artisan command. &lt;br&gt;&lt;br&gt;-- Laravel docs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's use this command to generate a rule that validates a string value of &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; as boolean.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="n"&gt;artisan&lt;/span&gt; &lt;span class="n"&gt;make&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;rule&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Laravel will place the new rule in the &lt;code&gt;app/Rules&lt;/code&gt; directory. If this directory does not exist, Laravel will create it when you execute the Artisan command to create your rule. &lt;br&gt;&lt;br&gt;-- Laravel docs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As promised, a &lt;code&gt;Boolean&lt;/code&gt; class is created in &lt;code&gt;app/Rules&lt;/code&gt; namespace, and here is what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Rules&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Contracts\Validation\Rule&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Rule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Create a new rule instance.
     *
     * @return void
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;passes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Get the validation error message.
     *
     * @return string
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'The validation error message.'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the &lt;code&gt;Boolean&lt;/code&gt; rule has been created, we are ready to define its behavior. &lt;br&gt;&lt;br&gt;
A rule object contains two methods: &lt;code&gt;passes&lt;/code&gt; and &lt;code&gt;message&lt;/code&gt;. The passes method receives the attribute &lt;em&gt;value&lt;/em&gt; and &lt;em&gt;name&lt;/em&gt;, and should return &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; depending on whether the attribute value is valid or not. The message method should return the validation error message that should be used when validation fails. &lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Small turn - make global helper functions
&lt;/h4&gt;

&lt;p&gt;But, just before doing that, it may be useful to extract &lt;code&gt;toBoolean&lt;/code&gt; from before to its own function and make it available globally.&lt;/p&gt;

&lt;p&gt;An easy and efficient way of creating global functions in Laravel is to autoload it directly from &lt;strong&gt;Composer&lt;/strong&gt;. The autoload section of composer accepts a &lt;code&gt;files&lt;/code&gt; array that is automatically loaded.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;helpers.php&lt;/code&gt; file wherever you like. I usualy keep my global helpers in &lt;code&gt;app/Support/helpers.php&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add your your helper functions, no need to specify any namespace &lt;em&gt;(so we don't have to use &lt;code&gt;use function&lt;/code&gt; to call them)&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;function_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'to_boolean'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Convert to boolean
     *
     * @param $booleable
     * @return boolean
     */&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;to_boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$booleable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;filter_var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$booleable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_BOOLEAN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_NULL_ON_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;In &lt;code&gt;composer.json&lt;/code&gt; inside the &lt;code&gt;autoload&lt;/code&gt; section add the following line &lt;code&gt;"files": ["app/Support/helpers.php"]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": ["app/Support/helpers.php"]
}
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;composer dump-autoload&lt;/code&gt; &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now our &lt;code&gt;to_boolean&lt;/code&gt; function is callable anywhere in our project.&lt;/p&gt;

&lt;p&gt;Back to our &lt;code&gt;Boolean&lt;/code&gt; rule.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Rules&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Contracts\Validation\Rule&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Rule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;passes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;is_bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;to_boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Get the validation error message.
     *
     * @return string
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'validation.boolean'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our case we can safly remove the constructor.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;is_bool&lt;/code&gt; function is a native php function, it finds out whether a variable is a boolean.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;__&lt;/code&gt; function is a Laravel strings helper, it translates the given translation string or translation key using your localization files.&lt;/p&gt;

&lt;p&gt;It's almost finished, all we have to do now is update our &lt;code&gt;PostRequest&lt;/code&gt; to implement the custom rule object &lt;code&gt;Boolean&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Rules\Boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'is_published'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Finally, our post has come to an end. So a quick recap, we have described two ways, approaches if you will, to validate &lt;code&gt;'true'&lt;/code&gt; and &lt;code&gt;'false'&lt;/code&gt; as boolean with Laravel validator.&lt;/p&gt;

&lt;p&gt;The first approach is preparing input for validation throughout the use of &lt;code&gt;prepareForValidation&lt;/code&gt; method provided to us by &lt;code&gt;FormRequest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second approach is using custom validation rules, more precisely rule objects, for that we have created our own &lt;code&gt;Boolean&lt;/code&gt; object to do the job.&lt;/p&gt;

&lt;p&gt;I know that I said that the first approach is easier to implement, but now that I use the rule object more often, I find it to be simpler and cleaner, the abstraction in rule object is more "developer-friendly" so to speak, the first approach is, arguably, more verbose. Either way, it's good to know them both, use whatever suits your use case or your personal preference. &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.is-bool.php"&gt;php.net - is_bool&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.php.net/manual/en/filter.filters.validate.php#filter.filters.validate"&gt;php.net - validate filters&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.filter-var.php#121263"&gt;php.net - filter_var - user contributed notes&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://stackoverflow.com/a/43243743/4325011"&gt;stackoverflow.com - How do I make global helper functions in laravel&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://stackoverflow.com/a/9115890/4325011"&gt;stackoverflow.com - The input value of false for FILTER_VALIDATE_BOOLEAN&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://github.com/laravel/ideas/issues/514"&gt;github.com - boolean validation does not accept "true" and "false", but accepts "1", "0"&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Originally published at &lt;a href="https://echebaby.com/blog/2021-12-30-laravel-validate-true-and-false-as-booleans/"&gt;https://echebaby.com&lt;/a&gt; on December 30, 2021.&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
    </item>
    <item>
      <title>Laravel - Store settings table in a configuration file</title>
      <dc:creator>Nour-Eddine ECH-CHEBABY</dc:creator>
      <pubDate>Tue, 13 Jul 2021 21:17:51 +0000</pubDate>
      <link>https://dev.to/chebaby/laravel-store-settings-table-in-a-configuration-file-2h</link>
      <guid>https://dev.to/chebaby/laravel-store-settings-table-in-a-configuration-file-2h</guid>
      <description>&lt;p&gt;If you find yourself in a situation where you need to store settings table in a configuration file, but in the same time you don’t want to sacrifice given the User/Admin the ability to update those settings when needed, then you’ve come to the right place.&lt;/p&gt;

&lt;p&gt;One of the reasons you may want to store your application settings in file is avoiding hitting the database too much.&lt;br&gt;
Well, I faced this problem myself in one of the projects I worked on. It was a relatively a small project. Storing my settings in a configuration file may not gain me too much in perfomance, but for my own peace of mind, I did it anyway.&lt;/p&gt;

&lt;p&gt;Here is schema definition of settings table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSettingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('settings', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('key')-&amp;gt;unique();
            $table-&amp;gt;text('value');
            $table-&amp;gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('settings');
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, it’s &lt;code&gt;key =&amp;gt; value&lt;/code&gt; combination.&lt;/p&gt;

&lt;p&gt;Well, the solution turns out to be pretty simple, all I had to do was taking adavantage of the &lt;a href="https://laravel.com/docs/8.x/eloquent#events"&gt;Eloquent Model Events&lt;/a&gt;. To be more precise I used &lt;code&gt;saved&lt;/code&gt; event, because the saving/saved events will dispatch when a model is &lt;u&gt;created&lt;/u&gt; or &lt;u&gt;updated&lt;/u&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is tested in Laravel 8 application, but I believe it will work well from Laravel version 5.5 and up.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Models;

use Illuminate\Support\Facades\File;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    use HasFactory;

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function booted()
    {
        static::saved(function () {

            $settings = static::pluck('value', 'key')-&amp;gt;toArray();

            $parsable_string = var_export($settings, true);

            $content = "&amp;lt;?php return {$parsable_string};";

            File::put(config_path('app_settings.php'), $content);
        });
    }

    //...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing complex is really going on here. First, throughout the Eloquent &lt;code&gt;pluck&lt;/code&gt; method we retrieve an &lt;code&gt;Illuminate\Support\Collection&lt;/code&gt; instance containing the keys/values.&lt;br&gt;
Chaining &lt;code&gt;toArray&lt;/code&gt; method converts the collection into a plain PHP &lt;code&gt;array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;PHP function &lt;code&gt;var_export&lt;/code&gt; returns a parsable string representation of the &lt;code&gt;$settings&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;I also use the &lt;code&gt;config_path&lt;/code&gt; function to generate a &lt;em&gt;fully qualified path&lt;/em&gt; to a file within the application’s configuration directory, a file I named &lt;strong&gt;app_settings&lt;/strong&gt;.php. You are free to change it to whatever suits you best.&lt;br&gt;
The &lt;code&gt;put&lt;/code&gt; method is used to store file contents on the disk.&lt;/p&gt;

&lt;p&gt;I did one more thing, I added &lt;code&gt;/config/app_settings.php&lt;/code&gt; to &lt;code&gt;.gitignore&lt;/code&gt; file.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://echebaby.com/blog/2020-12-20-laravel-store-settings-table-in-configuration-file/"&gt;https://echebaby.com&lt;/a&gt; on December 19, 2020.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
  </channel>
</rss>
