Call to a member function getClientOriginalName() on null How to upload image with create method?
Look at my codes.
public function store(Request $request)
{
if ($request->has('style_logo')) {
$style_logo = $request->file('style_logo');
$filename = $style_logo->getClientOriginalName();
$style_logo->move(public_path('public/images/styles'), $filename);
$style_logo = $request->file('style_logo')->getClientOriginalName();
}
Style::create([
'style_name' => $request->style_name,
'style_logo' => $style_logo,
'founder' => $request->founder,
'website' => $request->website,
]);
return redirect()->back();
}
I get this error.
Call to a member function getClientOriginalName() on null
@irankhosravi change the check to this one:
if ($request->hasFile('style_logo')) {
And initialize this: $style_logo before the if check.
$style_logo = null;
if..
Because you are using it in the create function, but it has no default value.
@nakov
I get this error gain,
Call to a member function getClientOriginalName() on null
Just use $filename to store the filename also since you already have that in a variable.
You are using getClientOriginalName
After move.
Not necessary.
@irankhosravi this doesn't work:
public function store(Request $request)
{
$filename = null;
if ($request->hasFile('style_logo')) {
$style_logo = $request->file('style_logo');
$filename = $style_logo->getClientOriginalName();
$style_logo->move(public_path('public/images/styles'), $filename);
}
Style::create([
'style_name' => $request->style_name,
'style_logo' => $filename,
'founder' => $request->founder,
'website' => $request->website,
]);
return redirect()->back();
}
@nakov that's what I said to use $filename at the bottom.
$filename = null;
Is unnecessary:
But that's okay because I realize your copy and paste code will also work.
I get this error.
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'style_logo' cannot be null (SQL: insert into styles (style_name, style_logo, founder, website, updated_at, created_at) values (bb, ?, b, bb, 2019-12-25 21:33:51, 2019-12-25 21:33:51))
@irankhosravi so your field in the database is not nullable.. You need to either set other default value to the filename or Validate the request to always contain an image.
Also there should be a file that because of this line
if ($request->hasFile('style_logo'))
Show your view.
Also don't create unless it has a file redirect somewhere.
@jlrdw not sure if you are noticing, but the create happens after the condition :) so that's why I told him to have a default value for the $filename and why the field should either be nullable or has a default file name.
@nakov
I upload an image and I see up error.
@nakov yes I caught that that's why I mentioned don't do the create unless it has a file. Or like you said have a default name to go in there.
@irankhosravi can you show your form.
You may be missing something there.
Did you include enctype="multipart/form-data"
<div class="form-group row">
<label for="style_logo" class="col-md-3 col-form-label text-md-right">{{ __('message.auth.style_logo') }}</label>
<div class="col-md-9">
<input id="style_logo" type="file" class="custom-file @error('style_logo') is-invalid @enderror" name="style_logo" >
@error('style_logo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<form action="{{ route('style.store') }}" method="post">
@csrf
include enctype="multipart/form-data" in your form tag. And the method.
Also don't forget you've uploaded files before maybe go back and look at one of your last examples.
Please sign in or create an account to participate in this conversation.