mozew's avatar
Level 6

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

0 likes
17 replies
Nakov's avatar

@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.

2 likes
mozew's avatar
Level 6

@nakov

I get this error gain,

Call to a member function getClientOriginalName() on null

jlrdw's avatar

Just use $filename to store the filename also since you already have that in a variable.

jlrdw's avatar

You are using getClientOriginalName After move.

Not necessary.

Nakov's avatar

@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();
}
1 like
jlrdw's avatar

@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.

mozew's avatar
Level 6

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))

Nakov's avatar

@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.

1 like
jlrdw's avatar

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.

Nakov's avatar

@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.

1 like
jlrdw's avatar

@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.

jlrdw's avatar

@irankhosravi can you show your form.

You may be missing something there.

Did you include enctype="multipart/form-data"

mozew's avatar
Level 6
        <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>
mozew's avatar
Level 6
    <form action="{{ route('style.store') }}" method="post">
              @csrf
jlrdw's avatar
jlrdw
Best Answer
Level 75

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 or to participate in this conversation.