Last active
October 7, 2020 22:26
-
-
Save quietconundrum/54eb68a659ac237e29c812ebb0496db2 to your computer and use it in GitHub Desktop.
laravel eloquent query
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
public function fetchTicket(Request $request) | |
{ | |
// Check fields | |
$validator = Validator::make($request->json()->all(), [ | |
'_token' => 'required' | |
]); | |
if ($validator->fails()) return $validator->messages()->first(); | |
// Format request | |
$data = $this->convertRequest($request->json()); | |
// Check token auth | |
if ($data['_token'] === md5(env('DISCORD_TOKEN'))) { | |
// Determine query | |
if (isset($data['latest'])) { | |
$query = DiscordSupportTickets::orderBy('ticket', 'DESC') | |
->limit(1) | |
->get(); | |
} | |
elseif (isset($data['ticket'])) { | |
$query = DiscordSupportTickets::where('ticket', $data['ticket']) | |
->limit(1) | |
->get(); | |
} | |
elseif (isset($data['user_id']) && isset($data['only_open']) && $data['only_open'] === true) { | |
$query = DiscordSupportTickets::where('user_id', $data['user_id']) | |
->where('open', 1) | |
->orderBy('ticket', 'DESC') | |
->get(); | |
} | |
elseif (isset($data['user_id']) && (!isset($data['only_open']) || $data['only_open'] === false)) { | |
$query = DiscordSupportTickets::where('user_id', $data['user_id']) | |
->orderBy('ticket', 'DESC') | |
->get(); | |
} | |
// Build results | |
if (isset($query) && sizeof($query)) { | |
return json_encode($query, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | |
} else { | |
return false; | |
} | |
} | |
else { return false; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment