1. Scope
It is a wrapper around a particular query. It we need to use a long query again and again then we can create a model inside model like below:
public function scopeIncomplete($query) {
return $query->when(‘is_completed’, 0);
}
Use:
App\Task::incomplete()->get();
2. Relation count and orderBy
District::withCount('ad')->orderBy('ad_count', 'desc')->get();
District has many ad. ad_count is dynamically created property containing total number of add.
3. Laravel hasMany with where
class Cars extends Eloquent
{
function photos()
{
return $this->hasMany('Photo')->where('photos.type', '=', 'Cars');
}
}
Labels: Laravel, Web development