Created
May 8, 2020 18:37
-
-
Save fabian71/3a5ca453b14d2f2be6c6e70941e51d3c to your computer and use it in GitHub Desktop.
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
| Tabela Pet | |
| *id Int | |
| *name String | |
| *gender Enum('m','f') | |
| *breed_id Int -> Chave estrangeira | |
| Tabela Breed (Raças) | |
| *id Int | |
| *name String | |
| O que eu preçiso é pegar raças e sexos da Tabela Pet, por exemplo: | |
| Pitbull - fêmea () | |
| Vira lata - macho () | |
| Estas raças e generos pegarei em um laço. | |
| Mas adicionei manualemnte para testar a query. | |
| Controller | |
| public function index(Request $request) | |
| { | |
| $user = \Auth::guard('api')->user(); | |
| $query = Pet::query(); | |
| $query = $this->onlyTrashedIfRequested($request, $query); | |
| $pets = $query->where('user_id', '!=' , $user->id)->orWhere->crush(7, 'f')->orWhere->crush(6, 'm')->where('active', 1)->orderBy('created_at', 'desc')->paginate(); | |
| return $pets; | |
| } | |
| Model | |
| public static function scopeCrush($query, $breed, $gender){ | |
| return $query->where('breed_id',$breed)->where('gender',$gender); | |
| } | |
| Este Scope Query esta me retornando a raça corretamente, mas ignorando o genero, | |
| Por exempo, esta retornando todos os Pitbull macho e femea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment