Skip to content

Instantly share code, notes, and snippets.

@vs9390
Created February 18, 2019 18:34
Show Gist options
  • Save vs9390/24a90f17ce815be42934c1c0313d3398 to your computer and use it in GitHub Desktop.
Save vs9390/24a90f17ce815be42934c1c0313d3398 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Models\Fortune;
use App\Models\World;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController {
public function json() {
return ['message' => 'Hello, World!'];
}
public function db() {
return World::find(random_int(1, 10000));
}
public function queries($queries = 1) {
$queries = $this->clamp($queries);
$rows = [];
while ($queries--) {
$rows[] = World::find(random_int(1, 10000));
}
return $rows;
}
public function fortunes() {
$rows = Fortune::all();
$insert = new Fortune();
$insert->id = 0;
$insert->message = "Additional fortune added at request time.";
$rows->add($insert);
$rows = $rows->sortBy("message");
return view("fortunes", ["rows" => $rows]);
}
public function updates($queries = 1) {
$queries = $this->clamp($queries);
$rows = [];
while ($queries--) {
$row = World::find(random_int(1, 10000));
$row->randomNumber = random_int(1, 10000);
$row->save();
$rows[] = $row;
}
return $rows;
}
public function plaintext() {
return response("Hello, World!")->header('Content-Type', 'text/plain');
}
private function clamp($value): int {
if (!is_numeric($value) || $value < 1) {
return 1;
} else if ($value > 500) {
return 500;
} else {
return $value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment