php - 在 Laravel 中保存新记录时无法增加列值吗?

标签 php mysql laravel eloquent

我在 Laravel 中创建了一个作业,以获取设备的所有位置。当设备启动位置发送时,每次设备有新位置时,都会将新记录插入到具有最新位置的数据库中。

每当为同一设备发送新位置时,我想增加计数字段。一切正常,除了 COUNT 列没有增加!!

这是我的工作代码

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Models\User;
use App\Models\Event;
use App\Models\Device;
use App\Models\Location;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Notifications\UpdatedMeta;

class SaveDataJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    private $request;
    private $ip;

    public function __construct($data)
    {
      $this->request = $data['request'];
      $this->ip = $data['ip'];
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
      $request = $this->request;

      // Try to identify the sender
      $device= $this->identifyDevice($request['meta']);

      // Fetch location data based on IP
      $locationData = $this->getLocationData($this->ip);
      $locationData['ip'] = $this->ip;

      // If we matched the sender, check if a location with the same data exists already. Otherwise create a new location
      if ($device) {
        $locationData['locatable_id'] = $device->id;
        $locationData['locatable_type'] = device::class;
        $location = $this->findLocation($this->ip, $device, $locationData);
      }

// the problem is here, I want to increment the count column when the device has new location
      if (isset($location)){
        $location->count++;
      }
      else {
          $location = Location::create($locationData);
      }

      $location->save();

      // Create and save events
      $this->saveEvents($request['events'], $request['meta'], $device);

      // Update device meta-data
      if ($device) $updated = $device->updateMeta($request['meta']);

      \Log::info("Handled data job");

    }

    private function identifyDevice($meta) {
      $device= null;

      // First try to identify with MAC address
      if (array_has($meta, 'mac')) {
        $device= Device::where('mac_wlan', $meta['mac'])->orWhere('mac_lan', $meta['mac'])->first();
      }

      // If not found, try to identify with device name
      if (!$device&& array_has($meta, 'device_name')) {
        $device= Device::where('device_name', $meta['device_name'])->first();
      }

      return $device;
    }

    private function getLocationData(String $ip) {
      $client = new Client(); //GuzzleHttp\Client
      $result = $client->get('http://ip-api.com/json/' . $this->ip);
      return json_decode($result->getBody()->getContents(), true);
    }

    private function findLocation($ip, $device, $locationData) {

        return Location::where('ip', $this->ip)
          ->where('locatable_id', $locationData['locatable_id'])
          ->where('locatable_type', $locationData['locatable_type'])
          ->first();
      }

}

位置表 enter image description here

最佳答案

尝试使用increment()

if ( $location ){
    $location->increment('count');
    $location->save():
}

关于php - 在 Laravel 中保存新记录时无法增加列值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718798/

相关文章:

php - 如何在 Laravel 5.5 中将 base64 图像上传到 S3 存储桶中

php - 如何在 Laravel 5 中通过 artisan 命令调用模型?

'if statements' 中的 PHP time() 未传递给 MySql 查询?

mysql - 如何在 Rails 中使用自定义 mysql 列类型 - varbinary(16)

php - Laravel 中 'id' 中的未知列 'where clause' 具有自定义 'id' 属性

php - 限制自己免于重载外部 API 的速率

php - 在 Doctrine ORM 中实现 "update if exists"

php - React+Laravel 5.8.33 +Axios : errors when registering user with an axios. post请求;澄清代码问题

php - 使用子字符串 PHP 在页面上显示 SQL 表中的 "Date"数据类型

mysql - 我是否正确理解了数据库设计?