Skip to content

Instantly share code, notes, and snippets.

@jimrubenstein
Created February 13, 2016 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimrubenstein/d64888d2ac30be208392 to your computer and use it in GitHub Desktop.
Save jimrubenstein/d64888d2ac30be208392 to your computer and use it in GitHub Desktop.
Extend/Manipulate the behavior of the Eloquent Query Builder to do custom things with your ORM-built queries
<?php namespace App\Http\Controllers\Party;
use App\Http\Controllers\Controller as BaseController;
use App\Party;
use Session;
use Redirect;
use Request;
use URL;
class Rsvp extends BaseController {
public function get($party_id)
{
$Party = Party::with(['Location', 'Rsvp'])->findByHashId($party_id);
if (!$Party)
{
return redirect('/');
}
return view('party.rsvp-form', [
'Party' => $Party
]);
}
}
<?php namespace App;
use Illuminate\Database\Eloquent\Model as BaseModel;
abstract class Model extends BaseModel {
public function newEloquentBuilder($query) {
return new QueryBuilder($query);
}
public function findByHashId($id, $columns = ['*'])
{
if (is_array($id))
{
return $this->findManyByHashId($id, $columns);
}
return $this->query->findByHashId($hash_id)->first($columns);
// return $this->first($columns);
}
public function findManyByHashId($ids, $columns = ['*'])
{
if (empty($ids)) {
return $this->model->newCollection();
}
$this->query->whereIn('hash_id', $ids);
return $this->get($columns);
}
}
<?php namespace App;
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
class QueryBuilder extends BaseBuilder {
public function findByHashId($hash_id, $columns = ['*'])
{
$this->query->where('hash_id', '=', $hash_id);
return $this->first($columns);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment