亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我嘗試在數據庫中存儲文件路徑名時,為什么會得到 null 返回值?

當我嘗試在數據庫中存儲文件路徑名時,為什么會得到 null 返回值?

PHP
侃侃無極 2023-07-08 20:58:27
我不確定為什么null在嘗試測試我的端點是否有效時會得到回報。表名是正確的,但由于某種原因,它無法識別我的文件。盡管我有代碼,但我的表有不止一列,并且是使用php artisan. (我只是想填充filePath列,稍后我將處理其他列)。下面是一張圖片供參考如果我刪除dd($filePath);控制器中的which - 郵遞員錯誤將返回:Integrity constraint violation: 1048 Column 'file_path' cannot be null (SQL: insert into 照片 (文件路徑) values (?))注意:我的 Laravel 代碼庫和 React.js 代碼庫是完全分開的。任何人都知道為什么會發生這種情況?這是我的前端代碼:fileSelectedHandler = event => {    this.setState({        selectedFile: event.target.files[0]    })}fileUploadHandler = () => {    const fd = new FormData();    fd.append('image', this.state.selectedFile, this.state.selectedFile.name);    axios.post('http://myendpoint/api/auth/wall-of-fame', fd)        .then(res => {            console.log(res);        })}<form action="http://myendpoint/api/auth/wall-of-fame" encType='multipart/form-data' id="login-form" className="form">            <input type="file" name="file" onChange={this.fileSelectedHandler}/>            <button onClick={this.fileUploadHandler}>Upload</button>        </form>這是我的 php 控制器:public function store(Request $request){    $filePath = $request->input('file')->getClientOriginalName();    $data=array('file_path'=>$filePath);    // dd($filePath);    DB::table('photos')->insert($data);    echo "Record inserted successfully.<br/>";    echo '<a href = "/insert">Click Here</a> to go back.';}
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

當從Request檢索文件時,使用filemethod 而不是inputmethod :

$filePath?=?$request->file('file')->getClientOriginalName();

您可以使用以下方法確定請求中是否存在文件hasFile

if?($request->hasFile('file'))?{
????$filePath?=?$request->file('file')->getClientOriginalName();
}
查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號