我正在嘗試設定一個簡單的按鈕來在單擊時更新 db 列值。我似乎無法弄清楚為什么我的路線沒有通過我的價值?
HTML:
<form method="post" action="{{ route('approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
控制器:
public function approveResturant($request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
路線:
Route::post('approveResturant'[ResturantController::class,'approveResturant'])->middleware(['auth'])->name('approveResturant');
最后,錯誤本身:

任何幫助表示贊賞!
uj5u.com熱心網友回復:
將Request型別提示添加到您的函式中:
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
這里的區別在于 Laravel 理解Request型別提示并且知道它Request應該從它在其服務容器中的預定義服務中注入物件。否則,Laravel 不知道該引數來自何處,因此假設您將提供它。簡單地命名您的引數$request是不夠的。
更新
您知道為什么該功能仍然不會將新批準的值保存到資料庫嗎?
幾個潛在的原因:
- 您尚未洗掉該
dd($request->all());宣告 $resturant = Resturant::find($id);未能在資料庫中找到記錄save是一個函式而不是一個屬性,所以$resturant->save;應該是$resturant->save();
要找出確切的問題,您需要執行一些除錯(例如,使用 xdebug 或dd陳述句)。
uj5u.com熱心網友回復:
使用請求類
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
uj5u.com熱心網友回復:
<form method="post" action="{{ route('restaurant.approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Route::post("/restaurant/store", [RestaurantController::class, "approveResturant"])->name("restaurant.approveResturant");
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
$restaurant = Restaurant::where("id", $request->input("id"))->update([
"approved" => 1
]);
return redirect()->back()->with('message', 'Restaurant Approved Successfully!');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513545.html
標籤:php拉拉维尔
上一篇:php表格顏色行按日期分組
