我想上传图片。 我为此编写了一个代码......但是这张照片根本没有添加到数据库中。只是," 其他"被执行。
public function store(Request $request){
//Get Request Input
$name = $request ->input('name');
$description = $request ->input('description');
$cover_image = $request ->file('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_filename,
'owner_id' => $owner_id
]
);
//Redirect
return \Redirect::route('gallery.index') -> with('message', 'Gallery Created');
}`
什么是错的?
答案 0 :(得分:0)
1)确保您在表单中添加了enctype="multipart/form-data"
,并在<input type="file">
字段中添加了name="cover_image"
2)在laravel images
文件夹中创建一个名为public
的新文件夹。
3)在controller
public function store(Request $request){
//Get Request Input
$name = $request ->input('name');
$description = $request ->input('description');
$owner_id = 1;
//Check Image Upload
if( $request->hasFile('cover_image')) {
$cover_image = $request->file('cover_image');
$path = public_path(). '/images/';
$cover_image_filename = $cover_image->getClientOriginalName();
$cover_image->move($path, $cover_image_filename);
}
else{
$cover_image_filename = 'noimage.jpg';
}
//Insert Gallery
DB::table('galleries')-> insert([
'name' => $name,
'description' => $description,
'cover_image' => $cover_image_filename,
'owner_id' => $owner_id
]);
//Redirect
return \Redirect::route('gallery.index') -> with('message', 'Gallery Created');
}
希望它有所帮助。