在我的代码中我尝试更新信息,但收到错误
Call to undefined method Illuminate\Database\Query\Builder::save()
$ user_id不是首选项表的主键。它只是一个列名
更新的控制器代码:
public function update(Request $request, preferences $preferences,$user_id)
{
$preferences = preferences::where('user_id', $user_id);
// $preferences = DB::table('preferences')->where('preferences.user_id','=', $user_id)->first();
$preferences->detergent= request()->detergent;
$preferences->fabricsoftener= request()->fabricsoftener;
$preferences->starch= request()->starch;
$preferences->save();
return redirect()->route('user.home.preferences', [Auth::user()->id]);
}
查看刀片文件中的代码
<form class="form-group coupon-form" action="{{ route('user.home.preferencesupdate', ['user_id' => Auth::id()]) }}" method="post">
{{ csrf_field() }}
<div class="col-md-12">
<div class="heading-title-alt text-left heading-border-bottom">
<h4 class="text-uppercase">Cleaning preferences</h4>
</div>
@foreach($preferences as $preferences)
<div class="form-group">
<label>Detergent</label>
<select name="detergent" class="form-control">
<option name="detergent" value="none" @if ($preferences->detergent == "none") selected @else "" @endif > None</option>
<option name="detergent" value="scented" @if ($preferences->detergent == "scented") selected @else "" @endif >Scented</option>
<option name="detergent" value="hypoallergenic" @if ($preferences->detergent == "hypoallergenic") selected @else "" @endif>Hypo-Allergenic</option>
</select>
</div>
<div class="form-group">
<label>Fabric Softener</label>
<select name="fabricsoftener" class="form-control">
<option name="fabricsoftener" value="yes" @if ($preferences->fabricsoftener == "yes") selected @else "" @endif>Yes</option>
<option name="fabricsoftener" value="no" @if ($preferences->fabricsoftener == "no") selected @else "" @endif>No</option>
</select>
</div>
<div class="form-group">
<label>Starch (Laundered & Press items only)</label>
<select name="starch" class="form-control">
<option name="starch" value="none" @if ($preferences->starch == "none") selected @else "" @endif>None</option>
<option name="starch" value="light" @if ($preferences->starch == "light") selected @else "" @endif>Light</option>
<option name="starch" value="medium" @if ($preferences->starch == "medium") selected @else "" @endif>Medium</option>
<option name="starch" value="heavy" @if ($preferences->starch == "heavy") selected @else "" @endif>Heavy</option>
</select>
</div>
@endforeach
<button class="btn btn-small btn-dark-solid full-width " id="login-form-submit" name="login-form-submit" value="login">Update</button>
</div>
</form>
请帮助我解决错误并解决它。
答案 0 :(得分:1)
你应该这样做:
$preferences = preferences::where('user_id', $user_id)
->update([
'detergent'=> request()->detergent,
'fabricsoftener'=> request()->fabricsoftener,
'starch'=> request()->starch,
]);
我希望它有所帮助。
答案 1 :(得分:0)
查询仍处于查询构建器状态,实际上是“获取”#39您将需要使用->get()
的模型,或者在您的->first()
中使用的模型,它本身不会返回模型。
preferences::where('user_id', $user_id)->first()