Contact Us

Creating an app to post tweets with Laravel and the Twitter API

Mobile App | May 29, 2021

In this tutorial we will build a fun little tool for internal/personal usage that posts random tweets at pre-defined times with Laravel and the Twitter API. We will use some of Laravel 4’s powerful features such as Artisan commands, Eloquent ORM and some external packages to make creating an application to post tweets with Laravel 4 quick and easy!

Our application will:

To post random tweets with Laravel we will need the following:

Setting up the project

First things first, you will need to create an application in your Twitter developer console here. Next we need to get the Laravel 4 Twitter API wrapper. We also want to make our life a bit easier and get the Laravel 4 generators by Jeffrey Way which will allow us to automate the creation of our basic code and rapidly scaffold our Laravel 4 tweet poster.  To do this, edit your composer.json file and add the packages.


Next, run composer update to install these packages. Open your app.php config file and edit the file to be able to use the Twitter API wrapper and the Laravel 4 generators.


Now in order to be able to use the Twitter API we must fill out the config file of the Twitter API wrapper. In your terminal, run the command php Artisan config:publish thujohn/twitter in order to publish the configuration files to the application level (your app folder).

You can now fill out the configuration file with your own Twitter API information:


Now that all of that is done there is one thing left to do to complete our configuration. Edit the app/config/database.php file with the information of your preferred database. Once this is done, we can move on to the actual code!

Defining the database and generating scaffolds

Now that everything is set up we can start thinking about our database and generate scaffolds. Scaffolds are “templates” that quickly prototype your application. Thanks to Jeffrey Way’s generators we only have to define our table columns and the generators do the rest for us!

We will need two tables, and only one field in each table (aside from the ID of course)! Aside from that we will need one extra pivot table which links the tweets and tags together, giving us three tables in total.

Let’s create our tweets scaffold. In your command line, enter the following Artisan commands:

Now we have some basic files and routes generated for us. In fact, we only have to make some small modifications now to be able to link tags to our tweets. In your tweets model, add a belongsToMany relationship.

The generator also added the following routes for us:


You can now use the tags() function to get all the tags that are attached to a tweet. Of course we still need to be able to attach tags to our tweets. First things first,  go to the tags view and create some tags there! Remember the generator already made the route for us, so we can simply go to http://[YOURAPP]/tags to create them. Same thing applies for the tweets.

Once you added some tags, we need to be able to select those tags in our tweets create view.

Modifying the controllers and views

In the TweetsController we need to select all tweets so we can display them in our view in a multiselect select element. To do this, we need to modify the create() function.


What we did here was get all the tags in a simple array form with the tag field as value, and the id as key.

And while we are here, we need to make sure our tags get stored alongside our tweets!


What we do here, is first save the tweet. If the tweet was saved, we use the sync() function to synchronize the links between our tweet and the tags. This function will add or remove rows as needed from the pivot table.

Now, we also need to do the exact same thing for edit() and update().


For the views, we simply add an extra select box with the multiselect property. We do this for both the files create.blade.php and edit.blade.php files.



As you can see this was really short and simple! You can now create or edit a tweet, and select some tags to be appended in a random order to the tweet. That was pretty easy huh? Now let’s move on to the final, and best part: Posting random tweets with an Artisan command.

Creating an Artisan command

To create an Artisan command you must go to your command line and execute the following command:

This will generate a new Artisan command file located in the commands folder. We need to fill out a name and description for our command now.


Now let’s think about how we can get a random tweet from our database the best possible way. ORDER BY RAND() is a bit slow so it is better to do a count of the records first, and then get a single row and use LIMIT.

We now have a tweet and its tags.  We will now prepare our data. I chose to add a new line and a separator inbetween my tweet and tags. The tags also have to be shuffled in order to keep our tweets more unique.

Now, we need to detect if there are any URLs in the tweet. This affects our tweet length limit since Twitter shortens URLs to 21 characters for HTTP and 22 for HTTPS. We will assume in this code that they are HTTP URLs.

Finally all that is left is to append the tags to the tweet body until you reach the length limit, and post that tweet! Everything put together:


Now if you use the command php artisan tweet:post you will post a random tweet from the database!

All that you have to do now is use crontab to execute the command every X minutes/hours/…

Share the knowledge!

This content was originally published here.