【问题标题】:Can't Increment column value when saving new record in Laravel?在 Laravel 中保存新记录时无法增加列值?
【发布时间】:2017-07-31 19:44:38
【问题描述】:

我在 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();
      }

}

位置表

【问题讨论】:

  • 你要计算的代码是多少?
  • $location-&gt;count = $location-&gt;count + 1; 有效吗?在增量前后回显$location-&gt;count 的值。有变化吗?
  • 我想在插入新记录时增加位置表中的计数字段` if (isset($location)){ $location->count++; } else { $location = Location::create($locationData); } $位置->保存(); ` @Hanny

标签: php mysql laravel eloquent


【解决方案1】:

尝试使用increment()

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

【讨论】:

  • 我厌倦了所有这些,没有工作!由于插入新记录而将其设置为 1 的原因,我认为首先我需要返回相同 locatable_id 的所有记录,然后 count(*) 设备计数字段然后增加它?对..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多