I happen to be a strong believer in the philosophy: learning by doing. In an attempt to learn Laravel 5, and also to live up to that philosophy, I decided to spend some time building a Questions-Answers web-app using Laravel 5. It is basically inspired by Stack Overflow, albeit it is way behind in term of features, it served its purpose well as a good learning experience. Here, I am going to give a quick tour to some of the pages in this web-app.
The homepage serves the users the recent questions (you can also filter for only open questions, and answered questions), paginated nicely using a a very handy feature that Laravel provides. In Laravel, we interact with database using something named Eloquent ORM. I am not going to explain all the technicality, but basically, first we need to create a Model for our database table. Each questions have tags associated to it, and users can get to the ask question form from the sidebar. They also can see which are the top 10 most used tags there.
As I was saying, we will be creating a Model for our database table. The Model class will extends the Eloquent Model, and it will automatically inherits a lot of the good methods we can use right out of the box! For example, once you build an Eloquent Model, you can easily retrieve
questions from the database, order it by date and paginate it with this one-liner:
$question = Question::orderBy('created_at', 'desc')->paginate(10);
Once your controller passes this data to the view file, the view file can easily call:
That’s it! That’s all the code you need to implement pagination! If you are not using Boostrap CSS though, you will only see the pagination navigator as
I had a mixed feelings about working with databases in Laravel. It actually really gets you going fast once you set it up properly. Making a query to database only takes a little bit of code, and it is very elegant! But it was quite hard for me to set it up. I guess it is because it was my first time working with a framework of this caliber. Basically, you need to setup database migrations script, then define the relationship in the Model files, and you’re done! It really isn’t that hard when you get used to it, and it offers you a lot of capabilities!
The question page
The question page just shows the question, followed by all the answers the other user posted. Users can vote on the question and the answers as well. Asker of question can accept an answer to his/her question. This is where most of the interaction in this web-app happens!
I only worked on this project for around 20+ hours, so there isn’t much to see here. But building it has gotten me to know more about some of the capabilities of Laravel, especially about middlewares, controllers, routing, eloquent model, schema builder, the blade template engine, database migration, and database seeding.
There is still A LOT to learn and utilize in Laravel such as events, event listeners, service providers, model factories, localization, authentication, and probably a whole lot more that I don’t even know about at this point of time.
I know I didn’t really explain anything technical in this post other than a little bit piece of code to implement pagination using Eloquent, but I hope it got you convinced that Laravel, while indeed have quite a steep learning curve, is not so bad after all!
For those who want to grab the code, you can get it on my GitHub repo. The instructions on how to set it up are also present in the readme file, so you can host your own web-app if you want (though I doubt you do).
You can also try the web-app live here.