【问题标题】:Why does Laravel / Eloquent's fresh function return an object, but calling it's attribute return 0?为什么 Laravel / Eloquent 的新函数返回一个对象,但调用它的属性返回 0?
【发布时间】:2017-04-12 19:45:21
【问题描述】:

我的代码在 Eloquent 中有一个地址 Model,它看起来像这样:

值得注意的是:我的所有模型都使用 uuid 作为主键,它是由 MySQL DB 中的触发器生成的。这本身就很有效。

<?php namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Address extends Model {    
        protected $table = 'Addresses';
        protected $fillable = ['uuid', 'zipCode', 'houseNumber'];
        protected $primaryKey = 'uuid';
        public $incrementing = false; //<- without this it returns 0, with it returns null
    }

在我的控制器中,我创建了一个函数来创建一个从触发器获取其 uuid 的新地址(如上所述)。要将这个 uuid(也是外键)传递给我的用户,我尝试调用 fresh();

<?php
public function store(Request $request) {
    $input = $request->all();
    try {
        $address = Address::create($input);
        // Call fresh to also get the trigger generated uuid primaryKey
        $address = $address->fresh(); //bug!!!
        //$address = Address::query()->where('zipCode', '=', $address->zipCode)->where('houseNumber', '=', $address->houseNumber)->first(); //works, but is not as nice
    }  catch (\exception $e) {
        $response['error'] = $e->getMessage();
        return response($response, 400);
    }
}

在重新加载(并获取我的 uuid)之前,$address 对象等于

object(App\Models\Address)#186 (23) {
  ["table":protected]=>
  string(9) "Addresses"
  ["fillable":protected]=>
  array(3) {
    [0]=>
    string(4) "uuid"
    [1]=>
    string(7) "zipCode"
    [2]=>
    string(11) "houseNumber"
  }
  ["primaryKey":protected]=>
  string(4) "uuid"
  ["incrementing"]=>
  bool(false)
  ["connection":protected]=>
  NULL
  ["keyType":protected]=>
  string(3) "int"
  ["perPage":protected]=>
  int(15)
  ["timestamps"]=>
  bool(true)
  ["attributes":protected]=>
  array(4) {
    ["zipCode"]=>
    string(6) "4651ZX"
    ["houseNumber"]=>
    string(2) "34"
    ["updated_at"]=>
    string(19) "2016-11-28 23:13:47"
    ["created_at"]=>
    string(19) "2016-11-28 23:13:47"
  }
  ["original":protected]=>
  array(4) {
    ["zipCode"]=>
    string(6) "4651ZX"
    ["houseNumber"]=>
    string(2) "34"
    ["updated_at"]=>
    string(19) "2016-11-28 23:13:47"
    ["created_at"]=>
    string(19) "2016-11-28 23:13:47"
  }
  ["relations":protected]=>
  array(0) {
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["casts":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(true)
}

如果在我的地址模型中定义了$incrementing = false;,我调用$address = $address-&gt;fresh(); 现在$address 对象等于NULL

如果我在我的地址模型中省略了$incrementing = false;,我打电话给$address = $address-&gt;fresh(); 现在$address 对象相等

object(App\Models\Address)#195 (23) {
  ["table":protected]=>
  string(9) "Addresses"
  ["fillable":protected]=>
  array(3) {
    [0]=>
    string(4) "uuid"
    [1]=>
    string(7) "zipCode"
    [2]=>
    string(11) "houseNumber"
  }
  ["primaryKey":protected]=>
  string(4) "uuid"
  ["connection":protected]=>
  NULL
  ["keyType":protected]=>
  string(3) "int"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["timestamps"]=>
  bool(true)
  ["attributes":protected]=>
  array(5) {
    ["uuid"]=>
    string(36) "0e85ea0c-b5c0-11e6-9c20-002590f967ca"
    ["zipCode"]=>
    string(6) "4651ZX"
    ["houseNumber"]=>
    string(2) "34"
    ["updated_at"]=>
    string(19) "2016-11-28 23:11:56"
    ["created_at"]=>
    string(19) "2016-11-28 23:11:56"
  }
  ["original":protected]=>
  array(5) {
    ["uuid"]=>
    string(36) "0e85ea0c-b5c0-11e6-9c20-002590f967ca"
    ["zipCode"]=>
    string(6) "4651ZX"
    ["houseNumber"]=>
    string(2) "34"
    ["updated_at"]=>
    string(19) "2016-11-28 23:11:56"
    ["created_at"]=>
    string(19) "2016-11-28 23:11:56"
  }
  ["relations":protected]=>
  array(0) {
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["casts":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
}

这就是我想要的!

但只要我调用$address['uuid']$address-&gt;uuid,它就会返回int(0)(非NULL)

【问题讨论】:

    标签: php mysql laravel-5 orm eloquent


    【解决方案1】:

    我必须用 uuid 作为字符串填充 casts 数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多