Reputation: 113
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.
Upvotes: 1
Views: 232
Reputation: 6259
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');
}
Upvotes: 2