Laravel 图片上传无效
Laravel Image Upload Not Working
我正在开发一个简单的 CMS,我在上传图片时遇到问题,因为它只是刷新 create/edit-page,而不是上传图片。当我注释掉图像部分时它起作用了。
这是我的代码:
public function create()
{
$packages = Package::all();
return view('packages.create')->withPackages($packages);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:30',
'content' => 'required|max:255',
'price' => 'required|max:15',
'icon' => 'required|image'
));
$package = new Package;
$package->title = $request->title;
$package->content = $request->content;
$package->price = $request->price;
if ($request->hasFile('icon')){
$image = $request->file('icon');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/' . $filename);
Image::make($image)->resize(300,300)->save($location);
$package->image = $filename;
}
$package->save();
return redirect()->route('packages.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$package = Package::find($id);
return view('packages.edit')->withPackage($package);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, array(
'title' => 'required|max:30',
'content' => 'required|max:255',
'price' => 'required|max:15',
'icon' => 'required|image'
));
$package = Package::find($id);
$package->title = $request->title;
$package->content = $request->content;
$package->price = $request->price;
if ($request->hasFile('icon')){
//Add new photo
$image = $request->file('icon');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/' . $filename);
Image::make($image)->resize(300,300)->save($location);
$oldFilename = $package->image;
//Update DB
$package->image = $filename;
//Delete the old photo
Storage::delete($oldFilename);
}
$package->save();
//Set flash data with success message
Session::flash('success', 'Pakkeløsningen blev opdateret!');
// Redirect with flash data to posts.show
return redirect()->route('packages.index');
}
这是我的 create.blade.php PackageController
{!! Form::open(['route' => 'packages.store']) !!}
{{ Form::label('title', 'Titel:') }}
{{ Form::text('title', null, array('class'=>'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('content', 'Indhold:') }}
{{ Form::text('content', null, array('class'=>'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('price', 'Pris:') }}
{{ Form::text('price', null, array('class'=>'form-control')) }}
{{ Form::label('icon', 'Vælg ikon') }}
{{ Form::file('icon') }}
{{ Form::submit('Lav pakkeløsning!!', array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top:20px;')) }}
{!! Form::close() !!}
Aaa 和我的路线,以防那是我遗漏的地方
Route::resource('packages', 'PackageController');
我的提供者和别名中确实有干预图像,并且我确实在 m 控制器中为它们命名空间,如下所示:
namespace App\Http\Controllers;
use App\Package;
use Session;
use Image;
use Storage;
提前致谢!我希望我不会用愚蠢的初学者问题来打扰任何人 :)
您缺少 enctype 属性。
编辑create.blade.php
表单打开如下
{!! Form::open(['route' => 'packages.store', 'enctype' => 'multipart/form-data']) !!}
我正在开发一个简单的 CMS,我在上传图片时遇到问题,因为它只是刷新 create/edit-page,而不是上传图片。当我注释掉图像部分时它起作用了。
这是我的代码:
public function create()
{
$packages = Package::all();
return view('packages.create')->withPackages($packages);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:30',
'content' => 'required|max:255',
'price' => 'required|max:15',
'icon' => 'required|image'
));
$package = new Package;
$package->title = $request->title;
$package->content = $request->content;
$package->price = $request->price;
if ($request->hasFile('icon')){
$image = $request->file('icon');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/' . $filename);
Image::make($image)->resize(300,300)->save($location);
$package->image = $filename;
}
$package->save();
return redirect()->route('packages.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$package = Package::find($id);
return view('packages.edit')->withPackage($package);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, array(
'title' => 'required|max:30',
'content' => 'required|max:255',
'price' => 'required|max:15',
'icon' => 'required|image'
));
$package = Package::find($id);
$package->title = $request->title;
$package->content = $request->content;
$package->price = $request->price;
if ($request->hasFile('icon')){
//Add new photo
$image = $request->file('icon');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/' . $filename);
Image::make($image)->resize(300,300)->save($location);
$oldFilename = $package->image;
//Update DB
$package->image = $filename;
//Delete the old photo
Storage::delete($oldFilename);
}
$package->save();
//Set flash data with success message
Session::flash('success', 'Pakkeløsningen blev opdateret!');
// Redirect with flash data to posts.show
return redirect()->route('packages.index');
}
这是我的 create.blade.php PackageController
{!! Form::open(['route' => 'packages.store']) !!}
{{ Form::label('title', 'Titel:') }}
{{ Form::text('title', null, array('class'=>'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('content', 'Indhold:') }}
{{ Form::text('content', null, array('class'=>'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('price', 'Pris:') }}
{{ Form::text('price', null, array('class'=>'form-control')) }}
{{ Form::label('icon', 'Vælg ikon') }}
{{ Form::file('icon') }}
{{ Form::submit('Lav pakkeløsning!!', array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top:20px;')) }}
{!! Form::close() !!}
Aaa 和我的路线,以防那是我遗漏的地方
Route::resource('packages', 'PackageController');
我的提供者和别名中确实有干预图像,并且我确实在 m 控制器中为它们命名空间,如下所示:
namespace App\Http\Controllers;
use App\Package;
use Session;
use Image;
use Storage;
提前致谢!我希望我不会用愚蠢的初学者问题来打扰任何人 :)
您缺少 enctype 属性。
编辑create.blade.php
表单打开如下
{!! Form::open(['route' => 'packages.store', 'enctype' => 'multipart/form-data']) !!}