Answer the question
In order to leave comments, you need to log in
How to upload an image to a Laravel CRUD model and store the result in a database?
When creating an entry or editing it, a temporary path is written in the field of the table called image instead of the file name. I have a function like this:
public function update(Request $request, Round $round)
{
$request->validate([
'name' => 'required'
]);
$image = $request->file('image');
$extension = $image->extension();
$imageName = $image->getClientOriginalName();
$image->move(storage_path('app/public/images/battles/battle'.$request->battle_id.'/round'.$request->rnum), $imageName);
$request->merge([
'image' => $imageName,
'status' => $request->has('status') ? true : false
]);
dd($request->all());
$round->update($request->all());
return redirect()->back()
->with('success', 'Запись обновлена.');
}
array:15 [▼
"_token" => "RDfCfKC368hhGem8R8XwJjqE9jllPZGmYaREfTPc"
"_method" => "PUT"
"name" => "Без сожалений"
"start" => "2021-04-07T01:54"
"end" => "2021-04-07T01:54"
"battle_id" => "2"
"rnum" => "4"
"title" => "tryrt"
"htitle" => "trytry"
"description" => "yrtyrt"
"keywords" => "rtytry"
"introtext" => "<p>rtytry</p>"
"content" => "<p>trytryt</p>"
"status" => true
"image" => Illuminate\Http\UploadedFile {#1450 ▼
-test: false
-originalName: "logo (2).jpg"
-mimeType: "image/jpeg"
-error: 0
#hashName: null
path: "/var/www/bezbflava.ru/data/mod-tmp"
filename: "phpavmPLE"
basename: "phpavmPLE"
pathname: "/var/www/bezbflava.ru/data/mod-tmp/phpavmPLE"
extension: ""
realPath: "/var/www/bezbflava.ru/data/mod-tmp/phpavmPLE"
aTime: 2021-04-07 13:40:30
mTime: 2021-04-07 13:40:30
cTime: 2021-04-07 13:40:30
inode: 3155818
size: 151782
perms: 0100600
owner: 510
group: 510
writable: false
readable: false
executable: false
file: true
dir: false
link: false
}
]
Answer the question
In order to leave comments, you need to log in
It was decided this way.
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Round $round
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Round $round)
{
$request->validate([
'name' => 'required'
]);
$request->merge([
'status' => $request->has('status') ? true : false
]);
$round->update($request->all());
if($request->has('image')) {
$image = $request->file('image');
$extension = $image->extension();
$imageName = $image->getClientOriginalName();
$image->move(storage_path('app/public/images/battles/battle'.$request->battle_id.'/round'.$request->rnum), $imageName);
$round->image = $imageName;
}
$round->save();
return redirect()->back()
->with('success', 'Запись обновлена.');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question