Laravel Inner加入Query Builder

时间:2014-03-15 09:25:37

标签: php laravel laravel-4

我在下面的代码中加入了webInformationweblinks以及webLink字段。

   $getResult = DB::table('webInformation')
        ->join('webLinks', function($join)
                {
                    $join->on('webLinks.id', '=', 'webInformation.weblink');
                })
        ->get();

此查询获取结果错误:

 Illuminate \ Database \ QueryException

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ads.weblinks' doesn't exist (SQL: select * from `webInformation` inner join `weblinks` on `weblinks`.`id` = `webInformation`.`weblink`

但我可以在phpMyadmin中获得成功。

模特: webInformation:

class webInformation extends Eloquent{

    protected $table='webInformation';
}

网络链接:

class webLinks extends Eloquent{

    protected $table='weblinks';
}

表:

phpMyadmin

所有表格和字段都是正确的。

toSql Command:

$getResult = DB::table('webInformation')
                ->join('weblinks', function($join)
                        {
                            $join->on('weblinks.id', '=', 'webInformation.weblink');
                        })
                ->toSql‬();

toSql Result:

BadMethodCallException

Call to undefined method Illuminate\Database\Query\Builder::toSql‬()

框架版本:

Laravel Framework version 4.1.23

1 个答案:

答案 0 :(得分:1)

您不需要使用DB::table()代替Eloquent方式:

$result = WebInformation::join('webInformation.weblink', '=', 'webLinks.id')->get();

或者这个:

$result = DB::table('WebInformation')->join('webInformation.weblink', '=', 'webLinks.id')->get();

这应该有效:

$sql = WebInformation::join('webInformation.weblink', '=', 'webLinks.id')->toSql();

您也可以使用它来记录上一个查询:

dd(DB::getQueryLog());
相关问题