laravel返回弹性青苗实例中的本地机器,但在整数JSON字符串 [英] laravel returns json string on local machine but integer on elastic beanstalk instance

查看:346
本文介绍了laravel返回弹性青苗实例中的本地机器,但在整数JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AWS,MySQL和laravel和角一个奇怪的问题。

我有一个无业游民实例都我的应用程序和数据库上运行本地运行。

我使用的角度上,所以当视图被加载角前端使得接收由用户输入的所有目标列表的请求。在目标字段之一是goalStatus。这是0或1存储为MySQL表整数。

角检查,如果这个值是0或1,取决于像这样的结果显示,不同的表格单元格

 百分位NG-IF =goal.goalStatus ==='0'>&LT,P类=TEXT-危险>在进度和LT; / P> < /第i
百分位NG-IF =goal.goalStatus ==='1'>&LT,P类=TEXT-成功>实现< / P>< /第i

在Chrome浏览器开发的工具,当我看着这个要求我看到返回goalStatus像这样的响应结果

 goalStatus:0

和if语句工作打算的角度。

然而,当我推这个程序,在弹性青苗一个开发环境,它连接到具有其开发工具相同的迁移和播种运行一个MySQL实例,RDS显示goalStatus因为这

 goalStatus:0

和角度,如果条件不具备这样的元素显示没有一个

如此看来,在弹性魔豆比如,它正在返回一个整数,但在本地机器上它是被作为一个字符串返回。我不知道天气的问题是在MySQL中,laravel,或其他地方。

什么想法?我已经包括了laravel迁移和种子类下表以防万一

迁移
    

 使用照亮\\数据库\\迁移\\迁移;
使用照亮\\数据库\\架构\\蓝图;类CreateGoalsTable扩展迁移
{
    / **
     *运行迁移。
     *
     * @返回无效
     * /
    公共功能了()
    {
        模式:创建('目标',函数($蓝图表){
            $表 - >的增量('身份证');
            $表 - >的整数('用户id');
            $表 - >字符串(标题);
            $表 - >字符串('goalDesc');
            $表 - >的整数('goalStatus') - GT;可空();
            $表 - >的整数('bodyGoalId') - GT;可空();
            $表 - >的整数('strengthGoalId') - GT;可空();
            $表 - >的整数('distanceGoalId') - GT;可空();
            $表 - >时间戳();
        });
    }    / **
     *反转迁移。
     *
     * @返回无效
     * /
    公共职能向下()
    {
        架构::降('目标');
    }}

播种机

 < PHP类GoalsTableSeeder扩展播种机
{
    公共职能的run()
    {
        //取消注释下面填充之前,擦桌子干净
        DB:表('目标') - GT;截断();        $现在=日期('Y-M-D H:I:S');        $目标=阵列(阵列(
            用户id => 1,
            '标题'=> 第一个进球标题',
            goalDesc'=> 这应该说明我的目标以文字形式,
            goalStatus'=> 0,
            bodyGoalId'=>空值,
            strengthGoalId'=>空值,
            distanceGoalId'=> 1,
            'created_at'=> $现在,
            '的updated_at'=>现在$)
            阵列(
                用户id => 1,
                '标题'=> 强度目标标题',
                goalDesc'=> 这应该说明我strngth目标文本形式,
                goalStatus'=> 0,
                bodyGoalId'=>空值,
                strengthGoalId'=> 1,
                distanceGoalId'=>空值,
                'created_at'=> $现在,
                '的updated_at'=>现在$)
            阵列(
                用户id => 1,
                '标题'=> 身体目标标题',
                goalDesc'=> 这应该说明我的身体的目标文本形式,
                goalStatus'=> 0,
                bodyGoalId'=> 1,
                strengthGoalId'=>空值,
                distanceGoalId'=>空值,
                'created_at'=> $现在,
                '的updated_at'=>现在$)
        );        //取消注释以下运行播种机
        DB:表('目标') - GT;插入($球);
    }}


解决方案

所以,这似乎是用PHP MySQL驱动程序返回的所有字段为字符串类型无关的问题。

我的AWS弹性青苗实例似乎是设置考虑到这一点,以便它返回字符串和字符串和整数为整数,而我需要的无业游民的设置更改为使用php5_mysqlnd驱动程序,而不是一个它有这解决了问题

I am having a strange problem with aws, mysql, laravel and angular.

I have a vagrant instance running locally with both my app and database running on it.

I am using angular on the front-end so when the view is loaded angular makes a request to receive a list of all 'goals' entered by the user. one of the fields in goals is goalStatus. this is either 0 or 1 stored as an integer in the mysql table.

angular checks if this value is 0 or 1 and displays and different table cell depending on the result like so

<th ng-if="goal.goalStatus === '0'"><p class="text-danger">In Progress</p></th>
<th ng-if="goal.goalStatus === '1'"><p class="text-success">Achieved</p></th>

in chrome dev tools when I look at the response result for this request I see goalStatus being returned like so

"goalStatus":"0"

and the angular if statements work as intended.

however when I push this app to a development environment in elastic beanstalk which connects to a mysql rds instance that has the same migrations and seeding run on it dev tools shows goalStatus as this

"goalStatus":0

and the angular if conditions are not met so neither one of the elements displays

So it seems that on the elastic beanstalk instance it is being returned as an integer but on the local machine it is being returned as a string. I dont know weather the problem would be in mysql, laravel, or somewhere else.

any ideas? I have included the laravel migration and seed classes for the table below just in case

migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateGoalsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goals', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('userId');
            $table->string('title');
            $table->string('goalDesc');
            $table->integer('goalStatus')->nullable();
            $table->integer('bodyGoalId')->nullable();
            $table->integer('strengthGoalId')->nullable();
            $table->integer('distanceGoalId')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('goals');
    }

}

seeder

<?php

class GoalsTableSeeder extends Seeder
{
    public function run()
    {
        // Uncomment the below to wipe the table clean before populating
        DB::table('goals')->truncate();

        $now = date('Y-m-d H:i:s');

        $goals = array(array(
            'userId' => 1,
            'title' => 'first goal title',
            'goalDesc' => 'This should describe my goal in text form',
            'goalStatus' => 0,
            'bodyGoalId' => null,
            'strengthGoalId' => null,
            'distanceGoalId' => 1,
            'created_at' => $now,
            'updated_at' => $now),
            array(
                'userId' => 1,
                'title' => 'strength goal title',
                'goalDesc' => 'This should describe my strngth goal in text form',
                'goalStatus' => 0,
                'bodyGoalId' => null,
                'strengthGoalId' => 1,
                'distanceGoalId' => null,
                'created_at' => $now,
                'updated_at' => $now),
            array(
                'userId' => 1,
                'title' => 'body goal title',
                'goalDesc' => 'This should describe my body goal in text form',
                'goalStatus' => 0,
                'bodyGoalId' => 1,
                'strengthGoalId' => null,
                'distanceGoalId' => null,
                'created_at' => $now,
                'updated_at' => $now)
        );

        // Uncomment the below to run the seeder
        DB::table('goals')->insert($goals);
    }

}

解决方案

So this appears to be an issue with the php mysql driver returning all fields as strings regardless of type. source

My aws elastic beanstalk instance seems to be setup to account for this so it is returning strings and strings and ints as ints whereas my vagrant setup needed to be changed to use php5_mysqlnd driver instead of the one it had and this resolved the issue

这篇关于laravel返回弹性青苗实例中的本地机器,但在整数JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆