Laravel 4 DB:在框架外运行时,raw()不可用

Laravel 4 DB:在框架外运行时,raw()不可用

问题描述:

I'm using the [illuminate/database component][1] from Laravel 4 through composer - and while it generally works well, the DB facade seems to be broken in this standalone version. This meant I was unable to use static functions such as DB::raw(). It seems like the DB facade is even included in the package, but it doesn't work with ::raw().

I'm trying to do something like this -

...->orderBy(DB::raw('RAND()'))

我正在使用Laravel 4中的[照亮/数据库组件] [1]到作曲家 - 虽然它一般 运行良好,数据库外观似乎在这个独立版本中被打破。 这意味着我无法使用DB :: raw()等静态函数。 看起来数据库外观甚至包含在包中,但它不适用于:: raw()。 p>

我正在尝试这样做 - p>

  ...-> orderBy(DB :: raw('RAND  ()'))
  code>  pre> 
  div>

Capsule::raw() is available, and is linked to the default connection's raw().

Also, what I did is I created a class:

/**
 * @method static raw($value)
 * @method static array select($query, $bindings = [], $useReadPdo = true)
 * ...etc.
 */
class DB extends Manager
{
}

so that

  1. You can use DB::raw().
  2. IDE code completion works.

I found a partial solution, but if anyone has any ideas that work better, I'm eager to hear them (it seems the original Capsule package actually had support built in, maybe it was lost when it was merged, or maybe I'm using it incorrectly?)

use Illuminate\Database\Capsule\Manager as Capsule;

$connection = Capsule::connection();

// You can now use $connection->raw() in place of DB::raw()
...->orderBy($connection->raw('RAND()'))