I'm trying to override Eloquent's Query Builder.
I have tried the following for my MyModel
:
<?php
class CustomQueryBuilder extends Illuminate\Database\Query\Builder
{
public function get($columns = array('*'))
{
die('get');
}
}
use CustomQueryBuilder as Builder;
class MyModel extends \Illuminate\Database\Eloquent\Model
{
// the model that needs the custom query builder
}
But when I run MyModel::get()
it still returns the object instead of dying.
Any ideas how to make this work?
I think the best way to do it is :
<?php
namespace MyNamespace\Models;
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
use \Illuminate\Database\Eloquent\Model;
class Builder extends BaseBuilder
{
public function like($key, $value){
return $this->orWhere($key, 'like', '%' . $value . '%');
}
}
class MyModel extends Model
{
protected $table = 'mytable';
public function newEloquentBuilder($query)
{
return new Builder($query);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With