Laravel Posts
How to create custom validation rules in Laravel ?Laravel

How to create custom validation rules in Laravel ?Laravel
While developing complex applications, sometimes we have to validate fields and data in a totally customized way, at that time you can use laravel's custom validations rules functionality.
In this tutorial, we are going to create our own custom validation rule to compare UUID. In our case, I have to check the UUID which is actually a binary string, whether it exists on DB or not.
Laravel doesn't provide any rule to compare that binary UUID string, so we will create our own validation rule.
So let's create our custom validation rule::
Generate Custom Validation Class
So here we have created a new class named UuidExists into App\Rules
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Ramsey\Uuid\Uuid;
class UuidExists implements Rule
{
protected $table;
protected $column;
public function __construct($table, $column)
{
$this->table = $table;
$this->column = $column;
}
public function passes($attribute, $value)
{
$value = Uuid::fromString(strtolower($value))->getBytes();
return \DB::table($this->table)->where($this->column, $value)->exists();
}
public function message()
{
return 'The validation error message.';
}
}
Add Rule to AppServiceProvider
Add your rule to AppServiceProvider.php into boot() method. here I have to give the name uuid_exists to my custom rule. you can give your own name whatever you want.
\Validator::extend('uuid_exists', function ($attribute, $value, $parameters, $validator) {
list($table, $column) = $parameters;
return (new UuidExists($table, $column))->passes($attribute, $value);
});
How to use custom rules?
You can use your custom rule as follows. Here we have using the required and uuid_exists rule, where we are passing attributes and values to our custom rule, which will be used to passes($attribute, $value) function.
'tenant_id' => ['required', 'uuid_exists:tenant_id,uuid']
Keep connected to us for more interesting posts about Laravel.
How to use One Signal in LaravelLaravel

How to use One Signal in LaravelLaravel
The OneSingnal is the market leader in push notification providers. It provides mobile + web push, email & in-app messages, and an easy way to send notifications. OneSignal provides official core PHP APIs but not the Laravel package. We are using OneSignal in many projects and write a bunch of line code in all projects where we needed OneSingnal.
One day I had an idea in my mind why I should not write a Laravel wrapper for OneSignal?. Finally, I wrote the shailesh-ladumor/one-signal Laravel Wrapper for it. Using this package, we can write neat & clean code and just a few lines of code.
OneSignal add this package in his official docs here
You can watch the following video tutorial or follow the article.
This package also works with the previous Laravel version.
Today we are going to see how we can use Laravel OneSignal Wrapper in Laravel. Let's see step by step, how we can do that.
Spet 1: Install Packages
Install shailesh-ladumor/one-signal by the following command,
composer require ladumor/one-signal
Step 2: Publish the config file
Run the following command to publish config file,
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Step 3: Add Provider
Add the provider to your config/app.php into the provider section if using a lower version of Laravel,
Ladumor\OneSignal\OneSignalServiceProvider::class
Step 4: Add Facade
Add the Facade to your config/app.php into aliases section,
'OneSignal' => \Ladumor\OneSignal\OneSignal::class
Configure a .env file with following keys
ONE_SIGNAL_APP_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
ONE_SIGNAL_AUTHORIZE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X
ONE_SIGNAL_AUTH_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ONE_SIGNAL_AUTH_KEY
is optional if you do not want to create an app. I hope you are familiar with the One Signal Platform and know how to get APP_ID and AUTHORIZE. If not, you should see the below image for how to get it.
So, we are done. Let's check how to send push notifications.
Check out this code to send a push notification.
use Ladumor\OneSignal\OneSignal;
$fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy']
$message = 'hey!! This is a test push.!' OneSignal::sendPush($fields, $message);