【发布时间】: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 ;
【问题讨论】: