繁体   English   中英

Laravel 5其中不起作用

[英]Laravel 5 WhereIn Not Working

我不知道为什么我的查询突然不起作用(尽管也许从一开始它就从来没有起作用)。 这里有什么问题吗?

我的控制器;

$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>";
}

我仍然得到相同的结果。 就像这个循环被完全忽略了-即使我可以使这些print / print_r正常工作。 原始SQL仍然只是select * from 201502_postcode

尝试这个

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

另外,如果这样不起作用,请尝试使第一个条件为whereIn ,其余条件为orWhereIn

哦,这是如此令人沮丧,我想我可能会哭...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM