gpt4 book ai didi

php - 如何在 Laravel 中将数据和文件、图像插入数据库和文件夹

转载 作者:行者123 更新时间:2023-11-29 06:56:03 25 4
gpt4 key购买 nike

我已经创建了一份注册表。我想将数据插入数据库,将文件、图像插入文件夹。当我插入数据和文件或图像时,它已成功插入。但是当我尝试不使用文件或图像时,它会出现以下错误:

(2/2) QueryException

SQLSTATE[HY000]: General error: 1364 Field 'fileimg' doesn't have a default value (SQL: insert into academic (name, username, pw) values (Kisalka Rajapaksha, 960863801V, $2y$10$u0Ryg3Yi9wYma.wm5piP2.1HFHKxruo6gZDU2Fo1onwQJhJmOWhG.))

我该如何解决这个问题?

Controller.php file.

function insert(Request $req)
{

$this->validate($req, [
'name' => 'required'
]);


if(Input::hasFile('file_img')){

$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$passen = bcrypt($password);
$file = Input::file('file_img');
$fileimg = $file->getClientOriginalName();
$destinationPath = 'img';

$rules = array(
'file_img' => 'required|max:10000|mimes:doc,docx,jpeg,png,jpg'
);

$validator = Validator::make(Input::all(), $rules);

if ($validator->fails()) {

// redirect our user back with error messages
$messages = $validator->messages();
// send back to the page with the input data and errors
return Redirect::to('RegShort')->withInput()->withErrors($validator);
return redirect('RegShort');

}

else if ($validator->passes()) {

$filemove = $file->move($destinationPath, $fileimg);

$dataimg = array("name"=>$name,"username"=>$username,"pw"=>$passen,"fileimg"=>$fileimg,"filemove"=>$filemove);

DB::table('academic')->insert($dataimg);

$req->session()->flash('Msg', 'Successfully Inserted !!');

return redirect('RegShort');

}
}

else
{
$names = $req->input('name');
$usernames = $req->input('username');
$passwords = $req->input('password');
$passens = bcrypt($passwords);

$data = array("name"=>$names,"username"=>$usernames,"pw"=>$passens);

DB::table('academic')->insert($data);

$req->session()->flash('Msg', 'Successfully Inserted !!');

return redirect('RegShort');
}


}

最佳答案

您应该将 fileimg 设置为迁移文件上的可为空字段。

$table->string('fileimg')->nullable();

这样您就可以在没有 fileimg 的情况下保存条目。

关于php - 如何在 Laravel 中将数据和文件、图像插入数据库和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45567409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com