Laravel 5 WhereIn Not Working

时间:2015-05-06 10:23:25

标签: php eloquent laravel-5

我不知道为什么我的查询突然不起作用(尽管可能从未开始工作)。这里有什么不对吗?

我的控制器;

$dataset = $postcode_lookup->dataset; // will return "201502_postcode"  

$postcode_extract = new PostcodeExtract;
$postcode_extract = $postcode_extract->setTableByDate($dataset);

foreach ($input as $column => $values) {
    $postcode_extract->orWhere(function ($query) use ($column, $values) {
                            $query->whereIn($column, $values);
                        });
}

/*
*  Temporarily print out the raw SQL...
*/
$sql = str_replace(['%', '?'], ['%%', "'%s'"], $postcode_extract->toSql());
$fullSql = vsprintf($sql, $postcode_extract->getBindings());
print_r($fullSql);

exit;

我的模特;

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PostcodeExtract extends Model {

protected $connection = 'postcodes';

public function setTableByDate($selected_tablename)
{
    $this->table = $selected_tablename;

    // Return $this for method chaining
    return $this;
}

public function getTable()
{
    if (isset($this->table))
        $this->setTableByDate($this->table);

    return $this->table;
}

原始sql的打印输出的所有内容都是select * from 201502_postcode,而忽略了查询的其余部分。

这是我的$input数组;

Array
(
[country] => Array
    (
        [0] => L93000001
        [1] => M83000003
    )

[county] => Array
    (
        [0] => 95
    )
)

orWhere和whereIn似乎被忽略了......

---更新---

我不知道orWhereIn。但是尝试了这个;

foreach ($input as $column => $values) {
        $postcode_extract->orWhereIn($column, $values);

        print "<br>";
        print $column;
        print "<br>";
        print_r($values);
        print "<br>";
}

我仍然得到相同的结果。就像这个循环被完全忽略一样 - 即使我可以让这些印刷品/印刷品工作。原始SQL仍然只是select * from 201502_postcode

2 个答案:

答案 0 :(得分:0)

试试这个

$postcode_extract->orWhereIn($column, $values)

此外,如果这不起作用,请尝试将第一个条件设为whereIn,其余条件设为orWhereIn

答案 1 :(得分:0)

哦,这太令人沮丧了,我想我可能会哭...

foreach ($input as $column => $values) {
        $postcode_extract = $postcode_extract->orWhereIn($column, $values);     
}