我有一個帶有文本字段和圖像字段的編輯表單,用戶可以根據需要編輯和上傳新文本或圖像。但如果用戶不上傳新圖像,我只想將舊圖像保留在數據庫中。我的問題問題是,當我點擊上傳按鈕時,我被重定向到文章頁面,并且沒有顯示錯誤,但數據庫尚未更新。任何幫助將不勝感激,因為我嘗試了多種方法但沒有成功。先感謝您。網頁.phpRoute::get('/tests/edit/{id}', 'TestController@edit');Route::patch('/tests', 'TestController@update');測試控制器.phppublic function edit(Request $request){ $test = Test::findOrFail($request->id); return view('test.edit', ['test' => $test]);}public function update(Request $request, Test $test){ $test->user_id = $request->user()->id; $test->name = $request->name; if($request->hasFile('image')) { Storage::delete('public/image/' . $test->image); //Delete old image $path = $request->file('image')->store('public/image'); $test->image = basename($path); } $test->update(); return redirect('/tests')}編輯.blade.php@extends('layouts.app')@section('content')<div> <form action="/tests" method="post" enctype="multipart/form-data"> {{ csrf_field() }} {{ method_field('patch') }} name:<input type="text" name="name" value='{{ $test->name }}'><br> image: <input type="file" name="image"><br> <input type='submit' value='upload'> </form></div>@endsection
2 回答

肥皂起泡泡
TA貢獻1829條經驗 獲得超6個贊
edit.blade.php 添加以下內容:
<input type="hidden" name="id" value="{{ $test->id }}">
測試控制器.php
use $test->save(); instead of $test->update();

青春有我
TA貢獻1784條經驗 獲得超8個贊
我和你有同樣的問題。我在一個使用 laravel 的訂購網站上工作,你可以從 laravel 獲取數據并將其顯示給用戶,他也可以從網站訂購一些東西,數據庫將被更新。但是,我也無法得到或更新數據庫中的數據。我使用 wampserver、mysql,一旦我在 phpmyadmin 中切換到 MariaDB 而不是 mysql,問題就解決了:http://localhost/phpmyadmin/
- 2 回答
- 0 關注
- 154 瀏覽
添加回答
舉報
0/150
提交
取消