Reputation: 33
I want to save an image on a directory and retrieve it on the webpage. I've tried several methods to store an image in a folder, but I got this error.
When I use $request->file('image')
result is null
I find out solutions and try as that solutions. But I can't fix it.
How can I fix this error and how can I save an image on a file directory
Controller
public function dili(Request $request)
{
$di = new diligent;
$di->jobtype = $request->jobtype;
$di->jobC = $request->jobC;
$di->details = $request->details;
$di->image = $request->image;
$imag = $request->image;
//dd($imag);
$path = $imag->getClientOriginalName();
$destinationPath = public_path('img');
$imag->move($destinationPath, $path);
$di->save();
$de = diligent::all();
return view('admin')->with('dw', $de);
}
web.php
Route::post('/admin', ['as'=> 'image.add', 'uses' => 'diligents@dili' ]);
admin.blade.php
<form class="row login_form" action="/admin" method="post"
novalidate="novalidate" enctype="multipart/form-data">
{{csrf_field()}}
<div class="col-md-12 form-group">
<input type="text" class="form-control" id="job" name="jobtype"
placeholder="Job " onfocus="this.placeholder =
''"onblur="this.placeholder = 'Job'">
</div>
<div class="col-md-12 form-group">
<input type="text" class="form-control" id="jobC" name="jobC" placeholder="Job Catagary" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Job Catagary'">
</div>
<div class="col-md-12 form-group">
<textarea class="form-control" name="details" id="details" rows="4" placeholder="Details" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Details'"></textarea>
</div>
<div>
<div class="col-md-12 form-group">
<p align="left">Upload Image</p>
<input type="file" name="image" id="image">
</div>
<div class="col-md-12 form-group col-xl-12">
<button type="submit" value="submit" class="primary-btn">Submit</button>
</div>
<div class="col-md-12 form-group col-xl-12">
<button type="submit" value="submit" class="primary-btn">Update</button>
</div></form>
This is the error message I got:
Call to a member function
getClientOriginalName()
onstring
Upvotes: 0
Views: 1076
Reputation: 132
Try this code for your project.
$image = $request->file('image');
$path = $image->getClientOriginalName();
dd($path);
By dd()
you find your image name.
And I hope the error will be removed.
Upvotes: 1