0

I'm running LVL 5.2.45 on two systems:

  • OSX 10.11.6, with PHP 7.0.13 (homebrew)
  • Ubuntu 14.04.5, with PHP 5.5.9-1ubuntu4.20

I am getting two different outputs from this code (eg. in Tinker):

App\Product::where('name', 'big brown bag')->get();

On OSX it outputs:

 App\Product {#744
 id: 54,
 name: "Big Brown Bag",
 category_id: 3,
 company_id: 1,
}

On Ubuntu:

 App\Product {#744
 id: 54,
 name: "Big Brown Bag",
 category_id: "3",
 company_id: "1",
}

Notice the double quotes around the ids in the Ubuntu output. The same outputs in a JSON.

How could I normalize this? Specifically, how could I force it to output the ids as integers?

Thanks in advance

1 Answer 1

1

As your case was quite interesting to me I spent some time searching for you a possible answer. Frameworks like Laravel use the PDO for working with different kind of DBMS and this kind of approach requires specific drivers.

I think that the problem is because of the php5-mysql library that is involved in the interaction with your MySQL database: this lib does NOT convert the number-type columns to a type number, leaving the Eloquent ORM with a string-type in return.

A possible solution is to replace the php5-mysql library with php5-mysqlnd (sudo apt-get install php5-mysqlnd). In fact the mysqlnd library returns a number type value instead of an insignificant string. As you're working with two different kind of PHP versions on your environments, I'm quite sure that it's just a matter of different libraries (and a different way of treating the value data type).

A quicker but dirt solution is to define the $casts protected property as an array where the key is the column and the value is the type you want that value to be casted. In example:

protected $casts = ['category_id' => 'integer']

This paragraph of the Laravel docs will be helpful to those are interested in this kind of approach

Anyway, this kind of problems should be avoided using a virtual machine packed for the dev team. Laravel offers an awesome virtual environment to use (I'm sure you've already heard of Homestead) that you can even use for other web-based applications, frameworks and CMS.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the time and the completeness of explaination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.