Get Specific Columns Using Eloquent Relationships In Laravel

Here’s a handy Laravel code snippet which allows you to select specific fields using Laravel’s Eloquent relationships. Lets say we have two tables, users and posts. A user has many posts while the post belongs to single user.

Then, in my User model I can write something like,

public function post(){
        return $this->hasmany('post');

This will fetch all the columns from post table that belongs to specific post. However, if you want to summon only a specified columns you can do that using the select method.

public function post(){
        return $this->hasmany('post')->select(['title','slug']);

Quick Tip : Set friendly names for validation attributes in Laravel

5 MVC Frameworks For PHP Developers in 2016

PHP is one of the most popular open source server side scripting language in the world. The reason behind its popularity is, it is very easy to learn and understand. PHP is more flexible when it comes to writing code because of its features like, weakly typed variables, OOP is not a mandatory.

However, there is a bad side of all this flexibility, it is very easy to write bad code. And the solution to this problem is FRAMEWORK.

There are many benefits of using a framework;

  1. Organization of code is very easy as most the frameworks follow MVC pattern
  2. Most of the frameworks have active community support.
  3. Faster development
  4. Form validation
  5. Pretty URLS

In this post, we are going to go over 5 most popular frameworks in PHP community.


Created by Taylor Otwell, Laravel is one the most popular framework. It is one of the most forked project framework on github with very active community support. Laravel’s features include RESTful routing, Blade templating engine, Eloquent ORM, Socialite, Middlewares etc.




Codeigniter was released in 2006, it is a lightweight PHP framework that comes with unparalled flexibilty and extraordinary documentation. This is the best framework for beginners as it is very easy to understand and learn.



Symfony is known as one of the best option for large scale enterprise applications. Licensed under MIT license, it was created by Sensiolabs in 2005. Symfony components are used by many popular open source projects including Drupal, PHPbb and Laravel.




CakePHP was initially in 2005 and is one of the most popular framework till date. some of the features of CakePHP are code generation and scaffolding, CSRF protection, form validations, built-in ORM, Helpers, Behaviours, and Plug-ins etc.




Yii is a open source PHP framework that follows the MVC pattern. It comes bundled with tons of features that include, a powerful caching system, form validations, integrated with jQuery, and AJAX-enabled features etc.


How to Remove a Package From Laravel Using Composer

We have already discussed how we can install packages and dependencies using Composer dependency manager. In this tutorial, we are going to see how we can remove existing packages from Laravel.

To remove a package navigate to your project folder and use the following command.

composer remove vendor/package


let’s say, I want to remove the HTML and Form builder package from my Laravel app. Then I need to run the following command.

composer remove laravelcollective/html

Where laravelcollective is the vendor name and html is the name of the package. Also, you need to remove references to that package from providers and aliases arrays from within the app.php.

Top 5 Free Code Editors For Web Developers

Code editors have a really heavy impact on the way developers right code. It plays an important role in reducing the errors and speeding up the coding process. There are tons of code editors out there with amazing features like syntax highlighting, code folding, directory tree view and many more which makes your coding much more fun. Here’s a list of top 5 code editors which I prefer to use.




Notepad++ is one of the most popular, open source and lightweight code editor available today. Personally, I have used Notepad++ for long time. It is very easy to use and comes with many built in features like Syntax highlighting, code folding etc.


Brackets is a free and open source editor from Adobe Systems. It is released under MIT License. It is avaliable for Windows, Linux and Mac operating systems.

Komodo Edit



Komodo Edit is a free code editor from ActiveState. It comes with a lots of amazing features like syntax highlighting, directory tree view, code folding and much more.


Atom is free and open source text editing application from Github. It is desktop application written in web technologies. It supports a wide variety of languages including PHP, Ruby, JavaScript, Objective-C etc.




Bluefish is another free code editor application distributed under GPL license. It comes with features like Syntax highlighting, Auto-completion and much more.

Check If Database Table Has Column in Laravel

Laravel comes with a handy built-in hasColumn method which allows the developers to determine whether a a specific column exists in a particular table or not.

You can use hasColumn method as follows;

if(Schema::hasColumn('users', 'email'))
    //do something if email column exists inside users table

The above code will check if an email column exists inside the users table.

Quick Tip : How to Override Default Controller Namespace in Laravel

How to Fix MassAssignmentException in Laravel

Mass assignment is a security vulnerability, which occurs when a malicious user passes unexpected HTTP parameters along with the request to the web server in order to gain access he is not supposed to.

Laravel comes with a built-in feature which allows you to protect your application against this security risk. In Laravel, you need to manually specify the list of database columns i.e variables you need to save inside the database using Eloquent’s create method which can be Mass Assigned. To specify the list of variables you need to use $fillable property inside your Model.


namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
    protected $fillable = ['first_name','last_name'];

For example, we can make first_name and last_name in Laravel Mass assignable by passing it to protected $fillable property in form of an array. You can also specify a list of database columns which cannot be mass assigned using the $guraded property in a similar way.

Whenever you try to save a variables using Eloquent’s create method which are not Mass assignable, Laravel will throw a MassAssignmentException error to your screen.

Read Also: Integrating Facebook Login into Laravel application

How to Install LAMP on Ubuntu

In this tutorial, we are going to install LAMP (Linux, Apache, MySQL and PHP) on Ubuntu OS.

To install LAMP on Ubuntu follow the steps given below,

Step 1:

The first thing we need to do is install Apache web server. To do this run the following command within your terminal.

sudo apt-get install apache2

Step 2:

In the second step, we are going to install MySQL. This can be done by running the following command.

sudo apt-get install mysql-server

Step 3:

Next thing we are going to do is install PHP on our computer. We do this by using following command.

sudo apt-get install php5 libapache2-mod-php5

Step 4:

By default, after installation Apache server automatically gets restarted. If it doesn’t run the following command.

sudo service apache2 restart

To check if the installation was successful, open your web browser and type following URL. After successful installation it will display a “It works” message.


Useful Read : How To Set Up mod_rewrite for Apache on Ubuntu

Time Ago Function in PHP

Here’s a lightweight PHP function which accepts a date as a parameter and converts it into “sometime age” output.

Read Also: How to enable mod_rewrite in Ubuntu

function time_ago($date) {
    if (empty($date)) {
        return "Please enter a date!";
    $duration_in = [
    $len = [
    $now = time();
    $unixDate = strtotime($date);

    if (empty($unixDate)) {
        return "Please enter a valid date!";

    if ($now > $unixDate) {
        $diff = $now - $unixDate;
        $tense = "ago";
    } else {
        $diff = $unixDate - $now;
        $tense = "from now";
    for ($j = 0; $diff >= $len[$j] && $j < count($len) - 1; $j++) {
        $difference /= $len[$j];
    $diff = round($diff);
    if ($diff != 1) {
        $duration_in[$j].= "s";
    return "$diff $duration_in[$j] {$tense}";


echo time_ago($date);