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.

This is my $input array;

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

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

orWhere and whereIn is just being ignored it seems..

--- UPDATE ---

I didn't know about 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. The raw SQL is still just select * from 201502_postcode.

Answers


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


Oh this is so frustratingly obvious I think I might cry...

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

Need Your Help

Phonegap / Cordova camera plugin - how to get photo's date/time stamp?

android ios cordova camera

I have a Phonegap app that needs to let the user both take photos using the phone's camera and let the user select from photo's already on the device.

For loop is only posting first 3 elements of array - PHP

php mysql

I have arrays which can potentially have unlimited amount of elements in them. I am trying to input them into my db but it only inputs the first 3 elements of the arrays.