Created
February 13, 2016 02:16
-
-
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
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\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 | |
]); | |
} | |
} |
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 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); | |
} | |
} |
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\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