【问题标题】:Lumen Eloquent save() don't update data at databaseLumen Eloquent save() 不会更新数据库中的数据
【发布时间】:2016-11-19 13:07:55
【问题描述】:

我有买家模型,但是当我想更改当前数据并保存时,它不会保存。 $buyer->save() 返回 1 但我数据库中的数据不会改变。

附: 我的数据库有 id、created_at 和 updated_at 字段。而且 $buyer 不是空的,它是我请求的数据对象('pCode'、'banned'、'hwid')

我的代码

namespace App\Http\Controllers;

use App\TGOBuyer as Buyer;

class SubController extends Controller
{
    public function ban($key){
        $buyer = Buyer::where('pCode', $key)->select('pCode', 'banned', 'hwid')->first();
        if (!$buyer || $buyer->banned)
            return response()->json(['wrong' => 'key']);

        $buyer->comment = 'test';
        $buyer->banned = 1;
        $buyer->save();
    }
}

买方模式 命名空间应用程序;

use Illuminate\Database\Eloquent\Model;

class TGOBuyer extends Model {
    protected $table = 'buyers';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'banned', 'comment', 'pCode', 'hwid'
    ];
}

更新 我试图返回 $buyer->id 并且它给了我 null,我不明白它为什么会发生。

这是我的数据库

CREATE TABLE IF NOT EXISTS `buyers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pCode` varchar(20) NOT NULL,
  `banned` tinyint(1) NOT NULL DEFAULT '0',
  `comment` text NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `pCode` (`pCode`),
  UNIQUE KEY `id_2` (`id`),
  KEY `id` (`id`),
  KEY `hwid` (`hwid`),
  KEY `pID` (`pID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=22468 ;

【问题讨论】:

    标签: php eloquent lumen


    【解决方案1】:

    我明白了。我只需要在我的选择查询中包含 id。现在一切正常。

    $buyer = Buyer::where('pCode', $key)->select('id', 'pCode', 'banned', 'hwid')->first();
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2012-08-29
      • 2020-10-01
      • 2018-04-09
      • 2014-08-28
      • 1970-01-01
      相关资源
      最近更新 更多