解决laravel id非自增 模型取回为0 的问题


Posted in PHP onOctober 11, 2019

问题

laravel5.2 中 如果一个模型的id 为string等非自增类型时候 使用模型的find方法 会返会0

样例代码:

$a=Model::find('blcu');
 echo $a->id; //结果为0

原因查找

通过var_dump(a)发现a)发现a

["attributes":protected]=>
 array(16) {
 ["id"]=>
 string(4) "blcu"

也就是数据其实是读取出来了 只是->id取得时候 变成了0

查看Model的 getAttribute 方法,此方法指向了 getAttributeValue

public function getAttributeValue($key)
 {
  $value = $this->getAttributeFromArray($key);


  if ($this->hasGetMutator($key)) {
   return $this->mutateAttribute($key, $value);
  }


  if ($this->hasCast($key)) {
   return $this->castAttribute($key, $value); //这一行是导致数值改变的地方
  }


  if (in_array($key, $this->getDates()) && ! is_null($value)) {
   return $this->asDateTime($value);
  }

  return $value;
 }

查看 castAttribute 如果 >getCastType(‘id') 如果为int 则 (int)$value

protected function castAttribute($key, $value)
 {
  if (is_null($value)) {
   return $value;
  }

  switch ($this->getCastType($key)) { 
   case 'int':
   case 'integer':
    return (int) $value; //这一行

查看 >getCastType

protected function getCastType($key)
 {
  return trim(strtolower($this->getCasts()[$key]));
 }

getCasts

最中改变值得代码:

public function getCasts()
 {

  if ($this->getIncrementing()) { //如果Model了的$incrementing字段为True
   return array_merge([
    $this->getKeyName() => 'int', //返回id=>'int'
   ], $this->casts);
  }

  return $this->casts;
 }

结论

Model的$incrementing 默认为true

当我们使用id为 非自增的时候 laravel 会把字符串转为int 所以输出了0

解决方案

给模型生命的时候添加

public $incrementing=false; 即可解决

以上这篇解决laravel id非自增 模型取回为0 的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
社区(php&&mysql)四
Oct 09 PHP
防止MySQL注入或HTML表单滥用的PHP程序
Jan 21 PHP
一个很不错的PHP翻页类
Jun 01 PHP
用php获取本周,上周,本月,上月,本季度日期的代码
Aug 05 PHP
探讨PHP中this,self,parent的区别详解
Jun 08 PHP
PHP采集类snoopy详细介绍(snoopy使用教程)
Jun 19 PHP
php实现使用正则将文本中的网址转换成链接标签
Dec 03 PHP
PHP SPL标准库之SplFixedArray使用实例
May 12 PHP
php实现微信公众平台账号自定义菜单类
Oct 11 PHP
如何使用PHP Embed SAPI实现Opcodes查看器
Nov 10 PHP
ThinkPHP中order()使用方法详解
Apr 19 PHP
laravel通过创建自定义artisan make命令来新建类文件详解
Aug 17 PHP
Thinkphp5.0框架的Db操作实例分析【连接、增删改查、链式操作等】
Oct 11 #PHP
PHP实现单条sql执行多个数据的insert语句方法
Oct 11 #PHP
laravel 中某一字段自增、自减的例子
Oct 11 #PHP
laravel框架的安装与路由实例分析
Oct 11 #PHP
php5与php7的区别点总结
Oct 11 #PHP
laravel 关联关系遍历数组的例子
Oct 10 #PHP
laravel请求参数校验方法
Oct 10 #PHP
You might like
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
浅谈使用 PHP 进行手机 APP 开发(API 接口开发)
2014/08/11 PHP
PHP Curl模拟登录微信公众平台、新浪微博实例代码
2016/01/28 PHP
php 替换文章中的图片路径,下载图片到本地服务器的方法
2018/02/06 PHP
php封装的page分页类完整实例代码
2020/02/01 PHP
对xmlHttp对象方法和属性的理解
2011/01/17 Javascript
javascript获取所有同类checkbox选项(实例代码)
2013/11/07 Javascript
JavaScript作用域链示例分享
2014/05/27 Javascript
jQuery实现径向动画菜单效果
2015/07/17 Javascript
js随机生成字母数字组合的字符串 随机动画数字
2015/09/02 Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
2015/11/29 Javascript
JavaScript的removeChild()函数用法详解
2015/12/27 Javascript
javascript作用域链与执行环境详解
2017/03/25 Javascript
Node.js发送HTTP客户端请求并显示响应结果的方法示例
2017/04/12 Javascript
详解vue.js移动端导航navigationbar的封装
2017/07/05 Javascript
详解vue2.0+vue-video-player实现hls播放全过程
2018/03/02 Javascript
vue实现点击展开点击收起效果
2018/04/27 Javascript
vue组件实现进度条效果
2018/06/06 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
对于Python中RawString的理解介绍
2016/07/07 Python
快速入手Python字符编码
2016/08/03 Python
python导入时小括号大作用
2017/01/10 Python
python实现俄罗斯方块
2018/06/26 Python
Python后台开发Django的教程详解(启动)
2019/04/08 Python
django使用admin站点上传图片的实例
2019/07/28 Python
Numpy将二维数组添加到空数组的实现
2019/12/05 Python
Python操作MongoDb数据库流程详解
2020/03/05 Python
Python 多线程C段扫描、检测 Ping扫描脚本的实现
2020/09/03 Python
Booking.com德国:预订最好的酒店和住宿
2020/02/16 全球购物
一套软件测试笔试题
2014/07/25 面试题
电焊工岗位工作职责
2014/07/09 职场文书
代收款委托书范本
2014/10/01 职场文书
公安机关查摆剖析材料
2014/10/10 职场文书
婚宴领导致辞
2015/07/28 职场文书
趣味运动会口号
2015/12/24 职场文书
php 文件上传至OSS及删除远程阿里云OSS文件
2021/07/04 PHP