Ad

How Can I Create Buttons With Laravel Using Foreach?

- 1 answer

could you help me, I'm creating a botman for telegram and the information I get from the database. What I need is to be able to create the buttons dynamically with a foreach or arrangement, I must clarify that this is not web, I do not use to create buttons, I use:

public function create ()
      {
      $ question = Question :: create ('Choose the headband of your interest:')
          -> fallback ('Choose an option, and it's time for the next question')
          -> callbackId ('Come on!')
          -> addButtons ([
              Button :: create ('Aastra') -> value ('a'),
          ]);

          return $ this-> ask ($ question, function (Answer $ answer) {
              if ($ answer-> isInteractiveMessageReply ()) {
                 } else {
                      $ this-> say (Inspiring :: quote ());
                        $ this-> askForDatabase ();
                  }
              }
          });
        }

In this piece of code is where I want to put the foreach or arrangement, this is in the Conversations.php folder and I call it in routes\botman.php so my bot can answer in the api telegram.

In case you do not understand what I want to do, can you tell me which part is not understood? since I'm not good at explaining.

Ad

Answer

I'd think what you want to do is pretty straightforward to achieve. Since you're getting the content of your buttons from the database, I'd assume you're dealing with objects. Therefore, I'd write it like below:

public function create ()
  {
  $objects = Data::all();
  $question = Question::create ('Choose the headband of your interest:')
      -> fallback ('Choose an option, and it's time for the next question')
      -> callbackId ('Come on!');
  foreach($objects as $object){
      $question->addButtons([
          Button::create($object->name)->value($object->value),
      ]);
  }

      return $this->ask($question, function (Answer $answer) {
          if ($answer->isInteractiveMessageReply()) {
             } else {
                  $this->say(Inspiring::quote());
                    $ this->askForDatabase();
              }
          }
      });
    }

With this, I believe you should have it working fine.

Ad
source: stackoverflow.com
Ad