php - Laravel 将 Model Save() 移动到方法内部
问题描述
目前我有这样的东西在我的Controller
:
$stock = Stock::first();
$stock->quantity = 10;
$stock->save();
我想把它改成这样,这样逻辑就在 中Model
,而不是在Controller
(因为我可能想从许多不同的地方调用它):
$stock = Stock::first();
$stock->updateQuantity(10);
我尝试将其添加到我的Stock
Model
:
public function updateQuantity($quantity){
$this->quantity = $quantity;
$this->save();
return true;
}
但它不起作用,我做错了什么?
解决方案
推荐阅读
- c++ - 消息离开复合模块时cMessage标签消失
- email - 我们可以更改 sendgrid 中的“apikey”字符串吗
- swift - UITableview 自定义单元格的 UIPanGestureRecognizer 不起作用
- android-studio - 我可以在 Android Studio 中创建自定义重新格式化代码吗
- c# - 检查具有给定总和的数组中的对
- dns - 使用 cPanel Transfer Tool 移动后旧服务器上出现错误 503(服务不可用)
- spring-boot - 如何配置 bootBuildImage 命令创建的镜像?
- python - django:在使用这种多对多关系之前,需要为字段“id”设置一个值
- ruby-on-rails - 按月排序日期而不计算年份 - Ruby on rails
- javascript - 尝试显示来自geojson的点时出现未捕获的错误:无效的LatLng对象:(NaN,NaN)