Created
December 11, 2016 14:04
-
-
Save anonymous/603865fa5e7f1dedcd9e6c7095d2c8c2 to your computer and use it in GitHub Desktop.
OrderBy e GlobalScope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
class Produto extends Model | |
{ | |
protected $table = 'produto'; | |
protected $dates = ['created_at', 'deleted_at','updated_at']; | |
use SoftDeletes; | |
protected static function boot() | |
{ | |
parent::boot(); | |
static::addGlobalScope('orderStatus', function(Builder $builder) { | |
$builder->orderby('status', 'desc'); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
use Request; | |
use App\Produto; | |
class ProdutoController extends Controller | |
{ | |
public function busca(){ | |
$produtos = Produto::with('imagens','categoria','loja','loja.cidade'); | |
$produtos = $produtos->orderby('titulo', 'DESC'); | |
$produtos = $produtos->get(); | |
//resultado (ordena primeiro por titulo) | |
// select * from `produto` order by `titulo` desc, `status` desc | |
// Gostaria que retornasse (ordenando primeiro por status) | |
// select * from `produto` order by `status` desc, `titulo` desc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment