Laravel save() 方法返回 true 但不更新记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29007088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel save() method returning true but not updating records
提问by Chris Townsend
I am using Eloquent to update my table Opportunity,
我正在使用 Eloquent 更新我的表 Opportunity,
Opportunity Model
机会模型
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Opportunity extends Model {
protected $primaryKey = 'OpportunityID';
protected $table = 'opportunitys';
// relationships
public function csfs()
{
return $this->hasMany('App\Csf', 'opportunityID');
}
public function benefits()
{
return $this->hasMany('App\Benefit', 'opportunityID');
}
public function risks()
{
return $this->hasMany('App\Risk', 'opportunityID');
}
public function projects()
{
return $this->belongsTo('App\Project', 'projectID');
}
public static function createNewOpportunity($input, $projectID)
{
$opportunity = new Opportunity;
$opportunity->value = $input['value'];
$opportunity->margin = $input['margin'];
$opportunity->duration = $input['duration'];
$opportunity->tender_type = $input['tender_type'];
$opportunity->likelihood_of_success = $input['likelihood_of_success'];
$opportunity->scope_of_work = $input['scope_of_work'];
$opportunity->deliverables = $input['deliverables'];
$opportunity->projectID = $projectID;
$opportunity->high_level_background = $input['high_level_background'];
if($opportunity->save())
{
Opportunity::leadSalesOppComplete($projectID);
return true;
};
}
public static function leadSalesOppComplete($projectID)
{
$task = Lead::where('projectID', '=', $projectID)->first();
$task->sales_opp = true;
return $task->save();
}
}
}
public function updateOpportunity(Request $request, $id) {
I get the id and find the opportunity.
我得到了 id 并找到了机会。
$something = Opportunity::find($id);
I have died and dumped this and I get this
我已经死了,扔掉了这个,我得到了这个
Opportunity {#259 ▼
#primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => 0
"margin" => 0
"tender_type" => ""
"likelihood_of_success" => 0
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
]
#original: array:12 [?]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [?]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
Which is correct. I then update these with
哪个是正确的。然后我更新这些
$something->margin = $request['margin'];
$something->duration = $request['duration'];
$something->tender_type = $request['tender_type'];
$something->likelihood_of_success = $request['likelihood_of_success'];
$something->scope_of_work = $request['scope_of_work'];
$something->deliverables = $request['deliverables'];
$something->high_level_background = $request['high_level_background'];
Now if I die and dump I get
现在如果我死了然后倾倒我得到
Opportunity {#259 ▼
#primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => "25000"
"margin" => "0"
"tender_type" => "Proposal"
"likelihood_of_success" => "0"
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
]
#original: array:12 [▼
"opportunityID" => 11
"value" => 0
"margin" => 0
"tender_type" => ""
"likelihood_of_success" => 0
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [?]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
I only changed the value which shows the change.
我只更改了显示更改的值。
I now run
我现在跑
$something->save();
It returns true when I die and dump it.
当我死并转储它时它返回真。
But no records are changed in the database.
但是数据库中没有记录被更改。
Any ideas?
有任何想法吗?
two images from tinker
修补匠的两张图片
回答by Chris Townsend
This line in the Opportunity model fixed the issue.
机会模型中的这一行解决了该问题。
Protected $primaryKey = "opportunityID";
Although it is difficult to understand why it was still possible to retrieve the data and create a new record.
尽管很难理解为什么仍然可以检索数据并创建新记录。
回答by kev
I had a very similar issue and this post lead me to a solution. I too was overriding a primaryKey
.
我有一个非常相似的问题,这篇文章引导我找到解决方案。我也覆盖了primaryKey
.
Environment:
环境:
- PHP 7.1
- Lumen 5.3 (obviously Eloquent enabled)
- Oracle 12 (using https://github.com/yajra/laravel-oci8)
- PHP 7.1
- Lumen 5.3(显然已启用 Eloquent)
- Oracle 12(使用https://github.com/yajra/laravel-oci8)
SELECT
and INSERT
operations work with protected $primaryKey = 'USER_ID';
, however, UPDATE
operations did not work even though save()
returned true
.
SELECT
和INSERT
操作一起工作protected $primaryKey = 'USER_ID';
,但是,UPDATE
即使save()
返回了操作也不起作用true
。
After finding this post I changed case. protected $primaryKey = 'user_id';
, Whammy, all three operations work! I wish I had a more solid explanation why this works. When I created the table I clearly used upper USER_ID VARCHAR2(50 BYTE) NOT NULL
.
找到这篇文章后,我改变了案例。protected $primaryKey = 'user_id';
, Whammy, 三个操作都有效!我希望我有一个更可靠的解释为什么会这样。当我创建表格时,我清楚地使用了 upper USER_ID VARCHAR2(50 BYTE) NOT NULL
。
回答by Anthony
I've had the same issue with Laravel 5.4and MySQL. My original code was:
我在Laravel 5.4和MySQL 上遇到了同样的问题。我的原始代码是:
$attach = new Attachments ;
$attach->site_id = ($type == 'site' ? $typeId : null) ;
...
By adding a null primary id, save()
behaves as expected.
通过添加 null 主 ID,save()
按预期运行。
$attach = new Attachments ;
$attach->id = null ;
$attach->site_id = ($type == 'site' ? $typeId : null) ;
...