logo
We provide creative solutions in form of web and mobile applications to clients around the world, creating apps that get attention and meaningful.

Web Development

Mobile Applications Development

Business Solutions

Odoo ERP Implementation

Wordpress Plugins

WordPress Themes

Laravel Development

Server Security

Server Administration

Mobile Marketing

Pay Per Click (PPC) Management

Conversion Rate Optimization

Email Marketing

Online Presence Analysis

Fell Free To contact Us
We are available from Monday to Friday 9AM to 6PM.

talk@appknitters.com

Lahore, Punjab, Pakistan

Top

Laravel Eloquent: Relationship with conditions and ordering

Many times you need to check against some condition for records returned by relationship in Laravel Eloquent. For example you only want a list of users which are approved or verified.
Normally we do it this way :



public function list_users() {
    return $this->hasMany('App\User');    
}

Then for returned users you need to iterate the list and check if it is approved or not.

But there is a short and easy way to do it:


public function verified_users() {
    return $this->hasMany('App\User')->where('verified', 1)->orderBy('email');
}

Having more than six years of experince developing custom applications with Laravel, WordPress, Zend, and Cordova.

No Comments

Post a Comment