Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中更改不同表中的时间僵尸_Php_Laravel_Postgresql - Fatal编程技术网

Php 如何在Laravel中更改不同表中的时间僵尸

Php 如何在Laravel中更改不同表中的时间僵尸,php,laravel,postgresql,Php,Laravel,Postgresql,你能给个主意吗? 我在postgresql数据库中有两个表。 记录由两个单独的csv文件填充。但是第一个csv文件的记录带有本地时间戳。第二个csv文件带有UTC时间戳。 因此,我需要更改此表中视图的本地时间,该表中填充了时区为UTC的记录。在使用laravel之前,我曾经处理过这个问题,我需要在每个页面中使用此代码,例如: date_default_timezone_set('Europe/Sofia'); 控制员: function getdata_chart(Request

你能给个主意吗? 我在postgresql数据库中有两个表。 记录由两个单独的csv文件填充。但是第一个csv文件的记录带有本地时间戳。第二个csv文件带有UTC时间戳。 因此,我需要更改此表中视图的本地时间,该表中填充了时区为UTC的记录。在使用laravel之前,我曾经处理过这个问题,我需要在每个页面中使用此代码,例如:

date_default_timezone_set('Europe/Sofia');
控制员:

      function getdata_chart(Request $request)
      {
        $start_date = date('d-m-Y 00:00:00');
        $end_date = date('d-m-Y 23:59:59');
        if($request->start_date != '' && $request->end_date != '')
        {
          // if user fill dates
          $dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
        } else {
          // default load page - today
          $dateScope = array($start_date, $end_date);
        };
        $students = MeasCanal::whereBetween('recordtime', $dateScope)
      ->selectRaw('recordtime')
      ->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Кота\') as iazovir')
      ->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Температура\') as temperatura350')
      ->where(function ($query) {
        $query->where('fullname', 'like', "Язовир.Данни.Кота")
              ->orWhere('fullname', 'like', "ГСК_11_350.Данни.Температура");
      })
      ->groupBy('recordtime')
      ->orderBy('recordtime')
      ->get();

        return response()->json($students);

      }
    return response()->json($students);
}

我不确定这是否是你想要的,但我认为你可以得到这个想法

第一种方法,

你可以使用

然后您可以获得如下值:
$model->custom\u timestamp

第二种方法,

你可以使用

编辑

在模型(MeasCanal)中设置记录时间属性

public function getRecordtimeAttribute($value)
{
    // you can use your logic here to set your timezone by table
    $timezone = 'Europe/Sofia';
    return Carbon::parse($this->recordtime)->timezone($timezone)->toDateTimeString();
}
然后,您只需在控制器中查看查询后的结果,如

dd($students)

或者更简单地说:

dd($students->first()->recordtime);//第一个匹配行记录时间属性。


注意:您不能在原始查询中使用访问器。顺便说一句,您应该使用雄辩的模型。

我不确定这是您想要的,但我认为您可以理解

第一种方法,

你可以使用

然后您可以获得如下值:
$model->custom\u timestamp

第二种方法,

你可以使用

编辑

在模型(MeasCanal)中设置记录时间属性

public function getRecordtimeAttribute($value)
{
    // you can use your logic here to set your timezone by table
    $timezone = 'Europe/Sofia';
    return Carbon::parse($this->recordtime)->timezone($timezone)->toDateTimeString();
}
然后,您只需在控制器中查看查询后的结果,如

dd($students)

或者更简单地说:

dd($students->first()->recordtime);//第一个匹配行记录时间属性。


注意:您不能在原始查询中使用访问者。您应该使用雄辩的模型。顺便说一句。

更新您的模型MeasCanal并添加以下内容:

进口和使用:

use Carbon\Carbon;
添加功能:

/**
     * Get the user's recordtime.
     *
     * @param  string  $value
     * @return string
     */
    public function getRecordtimeAttribute($value)
    {
            return Carbon::parse($value)->timezone('Europe/Sofia')->toDateTimeString();

    }

更新您的型号MeasCanal并添加以下内容:

进口和使用:

use Carbon\Carbon;
添加功能:

/**
     * Get the user's recordtime.
     *
     * @param  string  $value
     * @return string
     */
    public function getRecordtimeAttribute($value)
    {
            return Carbon::parse($value)->timezone('Europe/Sofia')->toDateTimeString();

    }


选中此处-如果需要更改默认时区,请在config/app.php上进行更改,你会发现关键点:时区更改为Europe/Sofiano我只需要在第二个表中更改它如果你不需要UTC时间戳,只需要创建一个脚本将所有UTC时间戳更新为本地时间戳。检查这里-如果你需要更改默认时区,请在config/app.php上更改它,你会发现关键点:时区更改为Europe/Sofiano如果你不需要UTC时间戳,我只需要在第二个表中更改它,而只需要创建一个脚本将所有UTC时间戳更新为本地时间戳。谢谢回复。我编辑了我的帖子。时间戳具有列名
recordtime
。你能用我的函数和你的第一个方法做个例子吗?我更新了我的答案,但刚刚意识到你是以json的形式发送响应的。在这种情况下,我建议您使用“地图”。谢谢您的回复。我编辑了我的帖子。时间戳具有列名
recordtime
。你能用我的函数和你的第一个方法做个例子吗?我更新了我的答案,但刚刚意识到你是以json的形式发送响应的。在这种情况下,我建议您使用“地图”。任何示例如何在上面的控制器中导入它,请?只需使用建议的信息编辑您的模型:MeasCanal,而无需更改控制器,我想它现在就可以工作了。非常感谢你,穆奇。现在它工作得很好!很高兴它能为你工作,祝你的项目好运。请举例说明如何在上面的控制器中导入它?只需编辑你的模型:MeasCanal和建议的信息,而不更改你的控制器,我想它会工作的。我现在看到了。非常感谢你,穆奇。现在它工作得很好!很高兴它为你工作,祝你的项目好运