2 回答
TA贡献1827条经验 获得超7个赞
在 html 表单中添加 enctype
enctype="multipart/form-data"
并在您的控制器中对此进行更改:
$img = Image::make($request->file('gambar')->getRealPath());
另请检查上传文件的目录的权限
TA贡献1803条经验 获得超3个赞
确保表单具有 enctype:
<form class="form" ... enctype="multipart/form-data">
变更控制器
use Intervention\Image\ImageManagerStatic as Image;
public function store(Request $request)
{
$this->validate($request, [
// 'judul' => 'required',
//if judul column type is varchar need to set max varchar value or less
//varchar max 255, if higer than 255 strings, extra string will be truncated
'judul' => 'required|string|max:200',
// 'category_id' => 'required',
//category should exist
'category_id' => 'required|exists:categories,id',
'konten' => 'required',
// 'gambar' => 'required',
//validating image is successfully uploaded and is image format
'gambar' => 'required|file|image|mimes:jpg,jpeg,png',
//validation for tags, assuming 1 input <select name="tags[]" multiple="multiple"/>
'tags' => 'array',
'tags.*' => 'exists:tags,id'//each value of input select exists in tags table
]);
// $gambar = $request->gambar;
//get the file from <input type="file" name="gambar"/>
$gambar = $request->file('gambar');
$new_gambar = time().$gambar->getClientOriginalName();
//make path to save image: sample public path
$file_path = public_path("uploads/post/{$new_gambar}");
$img = Image::make($gambar)
->resize(300,300)
->save($file_path);
$post = Posts::create([
'judul' => $request->judul,
'category_id' => $request->category_id,
'konten' => $request->konten,
// 'gambar' => 'public/uploads/posts/'.$new_gambar,
//should maake the image first
'gambar' => $file_path,
'slug' => Str::slug($request->judul),
'users_id' => Auth::id() // <- if it is to get current logged in user, use Auth::user()->id
]);
// $gambar->move('uploads', $new_gambar); //let Intervention do this for you
// $post->tags()->attach($request->tags);
//if tags exists (get values frominput select)
$post->tags()->sync($request->input('tags', []));
//$request->input('tags', []) <- if input tags is not null get value, else use empty array
//if route have name e.g Route::get('post', 'PostController@post')->name('post');
//return redirect()->route('post');
return redirect('post');
}
- 2 回答
- 0 关注
- 104 浏览
添加回答
举报