当我想将文件上传到我的数据库时,文件上传器给出空值
File uploader gives null when i want to sent it to my database
我试图在 laravel 中制作一个文件上传器。但是当我想将它发送到数据库时,当我将它发送到控制器时它有一个空值。我不知道我必须去哪里解决这个问题。
我的看法:
<div class="container">
<form method="POST" action="/admin/add-album/add">
@csrf
<label>Album name</label>
<input type="text" name="album_name" class="form-control">
<br>
<input id="file-upload" type="file" name="album_picture" accept="image/*">
<br>
<input type="submit" name="submit" class="form-control">
</form>
</div>
我的控制器:
public function addAlbumDatabase(Request $request)
{
request()->validate([
'album_name' => ['required'],
]);
$slug = $this->slugify(request('album_name'));
$user = \Auth::user();
dd($request->file('album_picture'));
if ($files = $request->album_picture) {
$destinationPath = 'public/images/';
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
} else {
dd("mislukt om de image up te loaden");
}
Albums::create([
'user_id' => $user->id,
'album_name' => request('album_name'),
'album_profile_picture' => $profileImage,
'album_slug'=> $slug
]);
return redirect('admin/add-album');
}
我的模特:
class Albums extends Model {
protected $fillable = [
'user_id', 'album_name', 'album_profile_picture', 'album_slug'
];
}
在表单规则中添加 enctype="multipart/form-data"
。所以:
<form method="POST" action="/admin/add-album/add" enctype="multipart/form-data">
我试图在 laravel 中制作一个文件上传器。但是当我想将它发送到数据库时,当我将它发送到控制器时它有一个空值。我不知道我必须去哪里解决这个问题。
我的看法:
<div class="container">
<form method="POST" action="/admin/add-album/add">
@csrf
<label>Album name</label>
<input type="text" name="album_name" class="form-control">
<br>
<input id="file-upload" type="file" name="album_picture" accept="image/*">
<br>
<input type="submit" name="submit" class="form-control">
</form>
</div>
我的控制器:
public function addAlbumDatabase(Request $request)
{
request()->validate([
'album_name' => ['required'],
]);
$slug = $this->slugify(request('album_name'));
$user = \Auth::user();
dd($request->file('album_picture'));
if ($files = $request->album_picture) {
$destinationPath = 'public/images/';
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
} else {
dd("mislukt om de image up te loaden");
}
Albums::create([
'user_id' => $user->id,
'album_name' => request('album_name'),
'album_profile_picture' => $profileImage,
'album_slug'=> $slug
]);
return redirect('admin/add-album');
}
我的模特:
class Albums extends Model {
protected $fillable = [
'user_id', 'album_name', 'album_profile_picture', 'album_slug'
];
}
在表单规则中添加 enctype="multipart/form-data"
。所以:
<form method="POST" action="/admin/add-album/add" enctype="multipart/form-data">