【问题标题】:laravel object to array, returning zero for some fieldslaravel 对象到数组,某些字段返回零
【发布时间】:2021-06-02 12:10:35
【问题描述】:

对 Eloquent 模型的非常简单的查询,以下是我在 tinker 中的锻炼。

在修补程序中我得到关注

    >>> $t1=Pomaster::all()->first()
     => App\Models\Pomaster {#3792
     ID: 77,
     poDate: "2021-05-02 18:41:50",
     vendorName: "OneVendor",
     poValue: 244132.0,
     poBy: "myGoodOrder",
     PONo: "PO-1",
     POStatus: 1,
     StoreID: "Johanesburg",
     Submitted: "2021-05-02 18:41:50",
     userName: "sumit.w",
     deleted: 0,
     lastupdate: null,
   }

当我查询它时,得到这个文件的正确答案

>>> $t1=Pomaster::all()->first()->poValue
=> 244132.0

但对于 PONo 字段,我得到零!

>>> $t1=Pomaster::all()->first()->PONo
=> 0

我被这个问题困扰了一段时间,找不到任何答案。 请帮忙 我在 Laravel 8 上,模型如下所示:

class Pomaster extends Model
{
    use HasFactory;
    public $table = 'pomaster';
    protected $primaryKey = 'PONo';

    public function poitem(){
        return $this->hasMany(Poitem::class,'PONo');
    }
}

【问题讨论】:

    标签: laravel eloquent laravel-8


    【解决方案1】:

    好的,经过某种冥想,我可以看出唯一的原因是“主键”。它是文本,是如何被推送到整数的

    所以早些时候我的模型是>>

    class Pomaster extends Model
    {
        use HasFactory;
        public $table = 'pomaster';
        protected $primaryKey = 'PONo';
    
        public function poitem(){
            return $this->hasMany(Poitem::class,'PONo');
        }
    }
    

    现在是它

    class Pomaster extends Model
    {
        use HasFactory;
        public $table = 'pomaster';
    //    protected $primaryKey = 'PONo';
    
        public function poitem(){
            return $this->hasMany(Poitem::class,'PONo');
        }
    }
    

    成功了,欢迎任何合乎逻辑的解释!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2019-11-03
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多