Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Eloquent is an Active Record implementation, not a Data Mapper implementation. What that means is it will not do the joins for you. If you want to do that sort of stuff without having to write the joins yourself then use something like Doctrine 2.

If you dont mind to write the joins by yourself try something like

/**
 * Sort model by parameters given in the URL
 * i.e: ?sortby=name&sortdir=desc
 *
 * @param Illuminate\Database\Eloquent\Builder
 * @return Illuminate\Database\Eloquent\Builder
 */
public function scopeOrderByUrl($query)
{
	$column = Input::get('sortby');
	$direction = (Input::get('sortdir') == 'desc') ? 'desc' : 'asc';

	return $query
	->select($this->getTable().'.*') // Avoid 'ambiguous column name' for paginate() method
	->leftJoin($table, $column, '=', "$table.id") // Include related table
	->orderBy("$table.$relatedColumn", $direction); // Sort by related column
}
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.