The image is not storing in the database
I already tried looking at other possible solutions but nothing work I also tried with using guessextension
public function store(Request $request)
{
$student = new student();
$this->validateRequest();
$student->name = $request->name;
$student->address = $request->address;
if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time() .'.'. $extension;
$file->move('uploads/student/',$filename);
$student->image = $filename;
}
else {
return $request;
$student->image ='';
}
$student->save();
return redirect()->route('show')->with('response', 'Registered Successfully');
}
Here is the form
@extends('layouts.app')
@section('content')
<div class="container">
<form method="get" action="/student" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="email">Name:</label>
<input type="text" class="form-control" name="name" id="email">
</div>
<div class="form-group">
<label for="pwd">Address:</label>
<input type="text" class="form-control" name="address" id="email">
</div>
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" >
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
@stop
What I need is the image to be stored in the database.
actually, when you upload an image to the server you save only the path of the image into your database
so firstly you need to Create a symbolic link from "public/storage"
to
"storage/app/public"
by artisan command: php artisan storage:link
after that, you can optimize this part of your code from this
if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time() .'.'. $extension;
$file->move('uploads/student/',$filename);
$student->image = $filename;
}
to this simple one
if ($request->hasFile('image')) {
$image = $request->image->store('uploads');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.