1. Go to this page and download the library: Download gengxuliang/laravel-sms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Gengxuliang\LaravelSms\Channels\EasySmsChannel;
use Gengxuliang\LaravelSms\Messages\EasySmsMessage;
class SmsCodeNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [EasySmsChannel::class];
}
/**
* 发送短信验证码
* @param $notifiable
* @return mixed
*/
public function toEasySms($notifiable)
{
return (new EasySmsMessage)
->setContent('模版消息传空就可以')
->setTemplate('SMS_164266880')
->setData(['code' => 3796]);
}
/**
* 记录到数据库
* @param $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'mobile' => $notifiable->mobile,
'content' => 3796,
'type' => 'code',
'template' => 'SMS_164266880',
];
}
}
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* 获取手机号
* @param $notification
* @return mixed
*/
public function routeNotificationForEasySms($notification)
{
return $this->mobile;
}
}