<?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: ssbhattarai</title>
    <description>The latest articles on DEV Community by ssbhattarai (@613596).</description>
    <link>https://dev.to/613596</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%2F468169%2F323806fa-aea3-47b0-bac2-7539620af8ac.jpeg</url>
      <title>DEV Community: ssbhattarai</title>
      <link>https://dev.to/613596</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/613596"/>
    <language>en</language>
    <item>
      <title>Create Custom laravel 8 rule to count the word of the request</title>
      <dc:creator>ssbhattarai</dc:creator>
      <pubDate>Tue, 31 Aug 2021 10:59:07 +0000</pubDate>
      <link>https://dev.to/613596/create-custom-laravel-8-rule-to-count-the-word-of-the-request-2m7a</link>
      <guid>https://dev.to/613596/create-custom-laravel-8-rule-to-count-the-word-of-the-request-2m7a</guid>
      <description>&lt;p&gt;Let’s use this command to generate a rule which helps to validate word count.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make: rule wordCount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create the file at &lt;strong&gt;App/Rules&lt;/strong&gt; with the name of &lt;strong&gt;wordCount.php&lt;/strong&gt; and initially, it will contain the following code:&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
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class WordCount implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
//
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return “Your message here.”;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy and paste the following code for word count validation:&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
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class wordCount implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
private $attribute;
private $expected;
public function __construct(int $expected)
{
$this-&amp;gt;expected = $expected;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this-&amp;gt;attribute = $attribute;
$trimmed = trim($value);
$numWords = count(explode(‘ ‘, $trimmed));
return $numWords &amp;gt;= $this-&amp;gt;expected;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return ‘The ‘.$this-&amp;gt;attribute.’ field must have more than ‘.$this-&amp;gt;expected.’ words’;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's import the above rule in controller as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Rules\wordCount;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally used this validation with any request with any minimun length of the request as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$request-&amp;gt;validate([
‘introduction’ =&amp;gt; [‘required’, new wordCount(50)],
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here the function &lt;strong&gt;wordCount&lt;/strong&gt; accepts the dynamic argument like i used 50 as minimum &lt;strong&gt;wordCount&lt;/strong&gt; for &lt;strong&gt;‘introduction’&lt;/strong&gt; request.&lt;/p&gt;

&lt;p&gt;Finally, use in blade as:&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;textarea type=”text” name=”introduction” class=”form-control” rows=”4" placeholder=”{{translate(‘Introduction’)}}” required&amp;gt;{{ $member-&amp;gt;member-&amp;gt;introduction }}&amp;lt;/textarea&amp;gt;
@error(‘introduction’)
@enderror
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All done, now it will display the error if it does not satisfy the wordcount like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The introduction field must have more than 50 words.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopes it works for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HAPPY CODING!!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>validation</category>
    </item>
    <item>
      <title>Permission denied for laravel 8 storage logs [solved] - 2022</title>
      <dc:creator>ssbhattarai</dc:creator>
      <pubDate>Mon, 23 Aug 2021 10:43:10 +0000</pubDate>
      <link>https://dev.to/613596/permission-denied-for-laravel-8-storage-logs-solved-2021-12ld</link>
      <guid>https://dev.to/613596/permission-denied-for-laravel-8-storage-logs-solved-2021-12ld</guid>
      <description>&lt;p&gt;If you are a laravel developer you may run into the &lt;strong&gt;laraval.log permission error that is **The stream or file "log file location" could not be opened: failed to open stream: Permission denied&lt;/strong&gt;. I found this error mostly while deploying laravel in VPS (virtual private network) in digitalocean droplet.&lt;/p&gt;

&lt;p&gt;To solve this error there is only one command which is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chown -R www-data:www-data /var/www/{laravel_project_name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your storage permission error must be solved by now.&lt;/p&gt;

&lt;p&gt;Happy Coding!!&lt;/p&gt;

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