Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用雄辩的语言加入Laravel 4_Mysql_Join_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Mysql 使用雄辩的语言加入Laravel 4

Mysql 使用雄辩的语言加入Laravel 4,mysql,join,laravel,laravel-4,eloquent,Mysql,Join,Laravel,Laravel 4,Eloquent,我有以下模型和相关的数据库表。 资源 标准 资源标准 我已经给了两个表一个正确的归属,所有的命名约定都是正确的。我正在尝试对查询执行联接,但我不断收到一个错误,即我要检查的字段不存在。因为我有几个值要检查,所以我将它们作为数组传递给查询生成器。以下是我构建查询的方式: $resource = Resource::where(function($query) use($values) { if($values["grade"] != 0) $query->where(

我有以下模型和相关的数据库表。 资源 标准 资源标准

我已经给了两个表一个正确的归属,所有的命名约定都是正确的。我正在尝试对查询执行联接,但我不断收到一个错误,即我要检查的字段不存在。因为我有几个值要检查,所以我将它们作为数组传递给查询生成器。以下是我构建查询的方式:

$resource = Resource::where(function($query) use($values)
{
    if($values["grade"] != 0)
        $query->where('grade_id', '=', $values["grade"]); 
    if($values['subject'] != 0)
        $query->where('subject_id', '=', $values['subject']);
    if($values['types'] != '')
    {
        if(is_array($values['types']) && count($values['types'])> 0)
            $query->whereIn('resourcetype_id', $values['types']);
        else
            $query->where('resourcetype_id', '=', $values['types']);
    }
    if($values['standards'] != '')
    {
        if(is_array($values['standards']) && count($values['standards'])> 0)
        {
            $query->join('resource_standard', 'resource_standard.resource_id', '=', 'resource.id')
                    ->with('standards')->whereIn('resource_standard.standard_id', $values['standards']);
        }
        else
        {
            $query->join('resource_standard', 'resource_standard.resource_id', '=', 'resource.id')
                    ->with('standards')->where('resource_standard.standard_id', '=', $values['standards']);
        }
    }
})->distinct()->take(30)->get();
当有一个标准的_id要检查时,它会给出以下错误:

{
    "error":{
                "type":"Illuminate\\Database\\QueryException",
                "message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'resource_standard.standard_id' in 'where clause' (SQL: select distinct * from `resources` where (`grade_id` = 2 and `subject_id` = 1 and `resource_standard`.`standard_id` in (4832, 4833)) limit 30)",
                "file":"\/Users\/luke\/Dropbox\/DEV\/PHP\/4aplus\/4aplus\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":555
            }
}

我通过使用
DB::table
而不是模型解决了这个问题。我想你也许不能和模特儿一起玩

$resource = DB::table('resources')->join('resource_standard', 'resource_standard.resource_id', '=', 'resources.id')

您也可以使用
雄辩的
模型加入
。只需使用以下代码:

$resource = Resource::join('resource_standard', 'resource_standard.resource_id', '=', 'resources.id')
与此相反:

$resource = DB::table('resources')->join('resource_standard', 'resource_standard.resource_id', '=', 'resources.id')

最后别忘了调用
->get()

您能显示出用于生成
$values
数组的相关代码吗?
$values[foo]
是否可能是
null
undefined
?这是不可能的,我取出联接并输出它正在生成的查询,值就在那里。它只是因为某种原因无法识别联接。您可以对模型查询执行相同的操作,错误表明您在那里有错误的列名。对,因为它无法识别联接。它没有看到连接的表,因此无法识别该列。这并不是因为它无法识别-模型查询是在与
DB::table
一起使用的同一查询生成器上构建的,因此出现了其他问题。