简体   繁体   English

Laravel 5其中不起作用

[英]Laravel 5 WhereIn Not Working

I have no idea why my query is suddenly not working (although maybe it was never working to begin with). 我不知道为什么我的查询突然不起作用(尽管也许从一开始它就从来没有起作用)。 Is there anything wrong here? 这里有什么问题吗?

My controller; 我的控制器;

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

My model; 我的模特

<?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;
}

All that the print out of the raw sql is returning is select * from 201502_postcode and is just ignoring the rest of the query. 原始sql的所有打印结果返回的内容都是select * from 201502_postcode ,而忽略了其余查询。

This is my $input array; 这是我的$input数组;

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

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

orWhere and whereIn is just being ignored it seems.. orWhere和whereIn只是被忽略。

--- UPDATE --- -更新-

I didn't know about orWhereIn. 我不知道orWhereIn。 But having tried this; 但是尝试过

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

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

I still get the same result. 我仍然得到相同的结果。 It's like this loop is being totally ignored - even though I can get these print's/print_r's to work. 就像这个循环被完全忽略了-即使我可以使这些print / print_r正常工作。 The raw SQL is still just select * from 201502_postcode . 原始SQL仍然只是select * from 201502_postcode

Try this 尝试这个

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

Also, if that doesnt work, try making the first one condition a whereIn and the rest a orWhereIn 另外,如果这样不起作用,请尝试使第一个条件为whereIn ,其余条件为orWhereIn

Oh this is so frustratingly obvious I think I might cry... 哦,这是如此令人沮丧,我想我可能会哭...

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