mardi 18 juin 2019

How to get or set `env` different value from a user in Laravel Controller

I am trying to set some of ENV values from the user end dynamically. I want to use multiple sms gateway services so that user can use any one from multiple gateways. Multiple services have different api, sid or token. I am saving these service providers credentials in sms_gateways table and trying to use these credentials from this table instead of hand code in controller method. But, I am getting in trouble to use like this. I am trying something like this -

public function storeSMS(Request $request)
{
    $smsGateway = $request->smsgatewayname;
    if ($smsGateway['value'] == 'Twilio') {
            $twilio = SmsGateway::where('name', 'Twilio')->first();

            //return $twilio->sid;
            //*here I want to use $twilio->sid value inside env. Is it possible?
            $sid    = env( 'TWILIO_SID' );
            $token  = env( 'TWILIO_TOKEN' );
            $client = new Client($sid, $token);
            $request->validate([
                'text'=>'required',
                'contact'=>'required'
            ]);

            $now = Carbon::now();
            $datetime = explode(' ', $now);


            $numbers_in_arrays = explode( ',' , $request->contact );
            $message = $request->text;
            $count = 0;
            foreach( $numbers_in_arrays as $number )
            {
                $count++;
                $client->messages->create(
                               '+'.$number,
                               [
                                   'from' => env( 'TWILIO_FROM' ),
                                   'body' => $message,
                               ]
                );

                $outbox = new SmsOutbox();
                $outbox->contact = '+'.$number;
                $outbox->text = $message;
                $outbox->send_date = $datetime[0];
                $outbox->send_time = $datetime[1];
                $outbox->is_send = 1;
                $outbox->save();
            }
    }
    return;
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Fhyx8k
via IFTTT

Aucun commentaire:

Enregistrer un commentaire