我正在使用Laravel并尝试建立画廊,我正在测试文件向db的上传,但是当我单击“提交”时出现错误 “照亮\数据库\ QueryException(23000) SQLSTATE [23000]:违反完整性约束:1048列
我已经设置了GalleryController,代码如下
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class GalleryController extends Controller
{
// List Galleries
public function index (){
//Render View
return view ('gallery/index');
}
// Show Create From
public function create(){
//Render View
return view ('gallery/create');
}
// Store Gallery
public function store(Request $request){
// Get Request Input
$name = $request->input ('name');
$description = $request->input ('description');
$cover_image = $request->input ('cover_image');
$owner_id = 1;
// Check Image Upload
if($cover_image){
$cover_image_filename = $cover_image->getClientOriginalName();
$cover_image->move(public_path('images'), $cover_image_filename);
} else {
$cover_image_filename = 'noimage.jpg';
}
//Insert Gallery
DB::table('galleries')->insert(
[
'name' => $name,
'description' => $description,
'cover_image' => $cover_image,
'owner_id' => $owner_id,
]
);
//Redirect
return \Redirect::route('gallery.index')-> with('message', 'Gallery Created');
}
//Show Gallery Photos
public function show($id){
die ($id);
`
main.blade.php使用以下代码调用代码
@ if(Session::has('message'))
<div class="alert alert-info">
{{Session::get('message')}}
</div>
@ endif;
我的.env数据库设置为root,密码也为空。
如果需要更多信息,请告知。
谢谢
答案 0 :(得分:0)
我认为您有两个错误: 1)description为null,因为输入内容为null或在视图文件中输入了另一个名称 您可以设置默认值吗?
$description = ($request->input ('description')) ? $request->input('description'): "description";
2)您的第二个错误是保存临时图像文件名
改用它
//插入图片库
DB::table('galleries')->insert(
[
'name' => $name,
'description' => $description,
'cover_image' => $cover_image_filename,
'owner_id' => $owner_id,
]
);