在 laravel 更新代码中调用未定义的方法 save()
Call to undefined method save() in laravel update code
在我的代码中尝试更新信息,但我收到错误
Call to undefined method Illuminate\Database\Query\Builder::save()
$user_id 不是首选项 table 的主键。它只是一个列名
更新的控制器代码:
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]);
}
查看 blade 文件中的代码看起来像
<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>
请帮我理解错误并解决它。
查询仍处于查询构建器状态,实际上 'get' 您需要使用的模型 ->get()
,或者在您的情况下 ->first()
,它自己还没有 return 模型。
preferences::where('user_id', $user_id)->first()
你应该这样做:
$preferences = preferences::where('user_id', $user_id)
->update([
'detergent'=> request()->detergent,
'fabricsoftener'=> request()->fabricsoftener,
'starch'=> request()->starch,
]);
希望对您有所帮助。
在我的代码中尝试更新信息,但我收到错误
Call to undefined method Illuminate\Database\Query\Builder::save()
$user_id 不是首选项 table 的主键。它只是一个列名
更新的控制器代码:
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]);
}
查看 blade 文件中的代码看起来像
<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>
请帮我理解错误并解决它。
查询仍处于查询构建器状态,实际上 'get' 您需要使用的模型 ->get()
,或者在您的情况下 ->first()
,它自己还没有 return 模型。
preferences::where('user_id', $user_id)->first()
你应该这样做:
$preferences = preferences::where('user_id', $user_id)
->update([
'detergent'=> request()->detergent,
'fabricsoftener'=> request()->fabricsoftener,
'starch'=> request()->starch,
]);
希望对您有所帮助。