Lumen Eloquent save() 不更新数据库中的数据

Lumen Eloquent save() don't update data at database

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

P.S。 我的数据库有 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'
    ];
}

更新 我尝试 return $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 ;

我明白了。我只需要将 id 添加到我的 select 查询中。现在一切正常。

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