1

When I use where on collection in Laravel 5.2 on local server I have to use integer value to filter values, but when I deploy on remote server I have to use string value on where function. Like this:

$indicators = Indicator::get();
$main_indicators = $indicators->where('main',1)->all(); 
//$main_indicators works on local empty on remote  

$indicators = Indicator::get();
$main_indicators = $indicators->where('main','1')->all(); 
//$main_indicators works on remote empty on local

This happens only when filtering a collection, created by Eloquent query. When I use where in eloquent query its working like this on every server:

Indicator::where('main',true)->get(); 

main is boolean (tinyInt(1) on MariaDB).

Migration:

$table->boolean('main')->default(false);

Local server: MacOSX, Xammp, PHP 7

Remote server: PHP 7, Apache

I want same code on every server, I want to change remote server, but I dont know what to set. Its really annoying develop two versions...

Database is the same on both servers.

Web server is Apache. Thanks

petkopalko
  • 600
  • 7
  • 18

2 Answers2

0

Okay, you're not using the where query properly. This needs to be specified prior to the get or all.

You're code should look like this (based on your current code):

$indicators = new Indicator; // Indicator::all() also works.
$main_indicators = $indicators->where('main',1)->get();

But that's ugly. Instead inject the Indicator class in your controller construct or the method itself. I'll use the controller injection method example below:

public function someFunction(Indicator $indictors) {
    $main_indicators = $indicators->where('main', true)->get();
}

Or just do:

$main_indicators = Indicator::where('main', true)->get();

Second example should be your go-to, IMHO ;)

Mike Barwick
  • 6,288
  • 6
  • 51
  • 76
0

The main problem was the MySQL driver: PHP + PDO + MySQL: how do I return integer and numeric columns from MySQL as integers and numerics in PHP?

But It can be really easily resolved by setting cast to model like this:

//Indicator model
protected $casts = [
    'main' => 'boolean'
];
Community
  • 1
  • 1
petkopalko
  • 600
  • 7
  • 18
  • If you prefer. But I consider this almost a hack to messy code. You shouldn't need to declare or cast the `main` attribute like that. – Mike Barwick Jun 20 '16 at 19:49