0

I've tried to update this excellent solution of Collin James to work in Laravel 4.

Until know without luck. This is what I came up with:

/app/libraries/Model.php (i've registered the libraries directory using composer)

namespace Elegant;

class Model extends \Illuminate\Database\Eloquent\Model {
    function __construct() 
    {
        echo 'Show me if the Model exension works<br />';
    }

    protected function query()
    {
        echo 'Show me if the query function gets called<br />';
        return new \Elegant\Query($this);
    }
}

/app/libraries/Query.php (i've registered the libraries directory using composer)

namespace Elegant;

class Query extends \Illuminate\Database\Query {

    public function __construct() 
    {
        echo 'Show me if the Query exension works<br />';
    }

    public function byArray($column, $value) 
    {
        if (is_array($value))
            return $this->whereIn($column, $value);
        else
            return $this->where($column, '=', $value);
    }

    public function __call()
    {
    }
}

/app/config/app.php

'aliases' => array(
    ...
    'Eloquent'        => 'Elegant\Model',
    ...
)

The only thing that works is:

  • "Show me if the Model exension works".

The other "markers" don't work:

  • query() doesn't get called at all
  • Eloquent/Query is used, instead of looking at Elegant/Query first
Community
  • 1
  • 1
Ronald Hulshof
  • 1,986
  • 16
  • 22
  • None of this is probably necessary, can you instead tell us what you want to do (as it's probably possibly without overwriting the method). – clone1018 Jun 12 '13 at 14:50

1 Answers1

0

Is QueryScopes what you mean? http://laravel.com/docs/eloquent#query-scopes You can add custom scopes for the query. You can also add extra parameters, but the $query is always added (and is the current query) Probably something like:

public function scopeByArray($query, $column, $value)
{
    if (is_array($value))
        return $query->whereIn($column, $value);
    else
        return $query->where($column, '=', $value);
}

And then just User::byArray($column, $value)->orderBy(..)

And you can just extend Eloquent and add functions there (class BaseModel extends Eloquent)

Barryvdh
  • 6,419
  • 2
  • 26
  • 23
  • Would it also be possible to set the $hidden and $visible properties in this way? http://stackoverflow.com/questions/16917051/laravel-eloquent-eager-loaded-hidden-visible-properties – Ronald Hulshof Jun 14 '13 at 11:51