Andreas94's avatar

Error getClientOriginalExtension in postEdit

I've placed an image upload in my post in "Add new". But when i go to modify the element, the upload me from problems, with the error:

Call to a member function getClientOriginalExtension() on null

This is my code:

public function postAdd

    $NomeImg = str_slug($slide->titolo, '-') . '.' .
    $request->file('immagine')->getClientOriginalExtension();
    $request->file('immagine')->move(base_path() . '/public/upload/news/', $NomeImg);
    $slide->immagine = $NomeImg;
add.blade
    {!! Form::label('Product Image') !!}
    {!! Form::file('immagine') !!}

While the edit function:

public function postEdit

    if ($request->hasFile('imagine')) {
      $NomeImg = str_slug($slide->titolo, '-') . '.' .
      $request->file('immagine')->getClientOriginalExtension();
      $request->file('immagine')->move(base_path() . '/public/upload/news/', $NomeImg);
      $slide->immagine = $NomeImg;
    }

How do I replace an existing image if another is loaded?

0 likes
11 replies
mushood's avatar

Hello, assuming you are using the same form.

I see this:

if ($request->hasFile('imagine')) {

with a SINGLE 'm'.

Shouldnt it be

if ($request->hasFile('immagine')) {

?

Andreas94's avatar

Thanks @mushood, i have correct in:

if ($request->hasFile('immagine'))

And although laravel does not give me any errors, it still does not work, The image is not even uploaded in the folder. Also, the query on the database is not updated...

/public/upload/news/

mushood's avatar

@Andreas94 for the file upload, confirm me something. Your file upload does not work BOTH when you add new and modify OR ONLY when you do modify.

for the update in the database, you need to call $slide->save()

Andreas94's avatar

I'm creating a page to customize the Boostrap carousel, Which requires you to add: Title, Description, Url and Image. When i add a new slider everything works, and uploads the image and save it in the database.

Only when I change a slider, if I modify: Title, Description or Url, it works. While if I try to change the image, no...

I did not put the full function because it is too long, however, here it is:

  public function postEdit(Request $request, $slideId) {
    $this->validate($request, [
      'titolo'  => 'required',
      'descrizione' => 'required',
      'url' => 'required'
    ], [
      'titolo.required' => "Inserisci il titolo.",
      'descrizione.required' => "Specifica la descrizione.",
      'url.required' => "Devi inserire un'url.",
    ]);
    $slide = Slide::find($slideId);
    $slide->titolo = $request->get('titolo');
    $slide->id_categoria = $request->get('id_categoria');
    $slide->descrizione = $request->get('descrizione');
    $slide->url = $request->get('url');

    if ($request->hasFile('immagine')) {
      $NomeImg = str_slug($slide->titolo, '-') . '.' .
      $request->file('immagine')->getClientOriginalExtension();
      $request->file('immagine')->move(base_path() . '/public/upload/news/', $NomeImg);
      $slide->immagine = $NomeImg;
    }
    $slide->save();
    return redirect('admin/slide')->with('success_message', 'Slide modificata correttamente.');
  }

edit.blade.php

<form action="" method="post">
  <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
    {{ Form::label('titolo', 'Titolo:') }}
    <input type="text" class="form-control" name="titolo" id="titolo" value="{{$slide->titolo}}" />
    {{ Form::label('descrizione', 'Descrizione:') }}
    <input type="text" class="form-control" name="descrizione" id="descrizione" value="{{$slide->descrizione}}" />
    {{ Form::label('url', 'Url:') }}
    <input type="text" class="form-control" name="url" id="url" value="{{$slide->url}}" />
  <div class="form-group">
    {!! Form::label('Product Image') !!}
    {!! Form::file('immagine') !!}
  </div>
    {{ Form::label('id_categoria', 'Categoria:') }}
    <select class="col-md-12 form-control" name="id_categoria" id="id_categoria">
      <option value="1"@if($slide->id_categoria=='1')selected="selected"@endif> Notizia</option>
      <option value="2"@if($slide->id_categoria=='2')selected="selected"@endif> Anteprima</option>
      <option value="3"@if($slide->id_categoria=='3')selected="selected"@endif> Intervista</option>
      <option value="4"@if($slide->id_categoria=='4')selected="selected"@endif> Recensione</option>
      <option value="5"@if($slide->id_categoria=='5')selected="selected"@endif> Soluzioni</option>
      <option value="6"@if($slide->id_categoria=='6')selected="selected"@endif> Speciale</option>
    </select>
    <button class="btn btn-success form-control">Salva Modifiche</button>
</form>
mushood's avatar

@Andreas94 In this part, can you change to this:

    if ($request->hasFile('immagine')) {
      $NomeImg = str_slug($slide->titolo, '-') . '.' .
      $request->file('immagine')->getClientOriginalExtension();
      $request->file('immagine')->move(base_path() . '/public/upload/news/', $NomeImg);
      $slide->immagine = $NomeImg;

//add this section
var_dump($NomeImg);die();
// you should now see the full path to see if the image file was actually //uploaded. Do let me know what you see here
    }
mushood's avatar

maybe the if check is not working correctly.

Try this instead:

if (isset($request->file('immagine')){
Andreas94's avatar

As by your advice, I inserted this string in the "if", but it seems to ignore it altogether... I tried to change the "if" as by your advice, but the result is the usual.

var_dump($NomeImg);
die();

That the problem is in the blade with the Form::file?

mushood's avatar
mushood
Best Answer
Level 41

You need to add this to your form: enctype="multipart/form-data"

You need to change this :

<form action="" method="post">

to

<form action="" method="post" enctype="multipart/form-data">

You should have this in your create.blade.php as well

1 like
mushood's avatar

Since you are using laravel collective, you could use this: Form::open(['url' => 'foo/bar', 'files' => true])

Updated form:

{{ Form::open(['url' => 'SAME AS IN YOUR ACTION', 'files' => true]) }}
    {{ Form::label('titolo', 'Titolo:') }}
    <input type="text" class="form-control" name="titolo" id="titolo" value="{{$slide->titolo}}" />
    {{ Form::label('descrizione', 'Descrizione:') }}
    <input type="text" class="form-control" name="descrizione" id="descrizione" value="{{$slide->descrizione}}" />
    {{ Form::label('url', 'Url:') }}
    <input type="text" class="form-control" name="url" id="url" value="{{$slide->url}}" />
  <div class="form-group">
    {!! Form::label('Product Image') !!}
    {!! Form::file('immagine') !!}
  </div>
    {{ Form::label('id_categoria', 'Categoria:') }}
    <select class="col-md-12 form-control" name="id_categoria" id="id_categoria">
      <option value="1"@if($slide->id_categoria=='1')selected="selected"@endif> Notizia</option>
      <option value="2"@if($slide->id_categoria=='2')selected="selected"@endif> Anteprima</option>
      <option value="3"@if($slide->id_categoria=='3')selected="selected"@endif> Intervista</option>
      <option value="4"@if($slide->id_categoria=='4')selected="selected"@endif> Recensione</option>
      <option value="5"@if($slide->id_categoria=='5')selected="selected"@endif> Soluzioni</option>
      <option value="6"@if($slide->id_categoria=='6')selected="selected"@endif> Speciale</option>
    </select>
    <button class="btn btn-success form-control">Salva Modifiche</button>
</form>

When you use the form::open, it also includes the csrf token.

DOC: https://laravelcollective.com/docs/master/html

1 like
Andreas94's avatar

Thank you so much @mushood! Can you explain a last thing I did not understand well?

{{ Form::open(['url' => 'SAME AS IN YOUR ACTION', 'files' => true]) }}

If my previous form was:

<form action="" method="post" enctype="multipart/form-data">

And my routers are:

Route::get('/', 'SlideController@getIndex');
Route::get('edit/{slideId}', 'SlideController@getEdit');
Route::post('edit/{slideId}', 'SlideController@postEdit');

In Url, should i leave blank? Thank you very much, the mistake was the forgetfulness of the enctype...

mushood's avatar

From what I can see, you dont have a submit button. Are you using JS to submit the form.

In that case, yes, you can leave the URL empty.

As a test, you can inspect the page in both cases. (right-click page in browser and then select "inspect". They should both give you the same results.

Also, please select as correct answer if it helped you :)

1 like

Please or to participate in this conversation.