Home > OS >  Laravel : Call to a member function getClientOriginalName() on null when Update Data
Laravel : Call to a member function getClientOriginalName() on null when Update Data

Time:10-06

I will update the data its contain image, then when I will send to update here's the error Call to a member function getClientOriginalName()

This is my Controller

 public function updateBook(Request $request, $id){
    
    $book =   DB::table('books')->where('id',$id);
    $old = $book->select('images');

    $ChecK = 0;
    $file = var_dump($request->file('images')->getClientOriginalName());
    $filename = time().$file;
    $path = public_path('/Uploads');
    
    if($request->hasFile('images')){
        $file = $request->file('images');
        $file->move($path, $filename);
        Storage::delete($old);
        $ChecK = 1;
    }
    else {
        dd('Request Hash No File!');
    }
    $data = [
        'name'=> $request->input('bookName'),
        'year'=> $request->input('tahunT'),
        'author'=> $request->input('author'),
        'summary'=> $request->input('Summary'),
        'publisher'=> $request->input('publishers'),
        'pageCount' => $request->input('pageCount'),
        'readPage'=> $request->input('ReadPage'),
        'finished' => '$pageCount' == '$readPage' ? true : false,
        'reading' => '$readPage' > 0 ? true : false,
        'updatedAt'=> new DateTime()
    ];
    if( $ChecK == 1){
        $data = ['images' => $filename];
        $book->update($data);
        return redirect('home')->with('status', 'update succesfully'); 
    }
    else {
        $data = ['images' => $old];
        $book->update($data);
        return redirect('home')->with('status', 'Update with old image '); 
    }
}

Here is the view

   <div class="card-body">
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif
                @foreach ($toEdit as $dt)
                <form class="row g-3" enctype="multipart/form-data" method="POST" action="{{ route('update', $dt->id) }}" value="PUT">
                    {{ csrf_field() }}
                    <div class="col-md-6">
                        <label for="bookName" class="form-label">Nama Buku:</label>
                        <input type="text" class="form-control" name ="bookName"id="bookName" value="{{ ($dt->name) }}">
                    </div>
                    <div class="col-md-3">
                        <label for="tahunT" class="form-label">Tahun Terbit : </label>
                        <input type="number" class="form-control" name="tahunT" id="tahunT" value="{{ ($dt->year) }}">
                    </div>
                    <div class="col-md-3">
                        <label for="author" class="form-label">Author : </label>
                        <input type="text" class="form-control" name="author" id="author" value="{{ ($dt->author) }}">
                    </div>
                    <div class="col-md-6">
                        <label for="publishers" class="form-label">Publisher : </label>
                        <input type="text" class="form-control" name="publishers" id="publishers" value="{{ ($dt->publisher) }}">
                    </div>
                    <div class="col-md-3">
                        <label for="pageCount" class="form-label">Page Count :</label>
                        <input type="number" class="form-control" name="pageCount" id="pageCount" value="{{ ($dt->pageCount) }}">
                    </div>
                    <div class="col-md-3">
                        <label for="ReadPage" class="form-label">Read Page :</label>
                        <input type="number" class="form-control" name="ReadPage" id="ReadPage"value="{{ ($dt->readPage) }}">
                    </div>
                    <div class="col-12">
                        <label for="Summary" class="form-label">Summary :</label>
                        <textarea class="form-control" name="Summary" id="Summary">{{ ($dt->summary) }}</textarea>
                    </div>
                    <div class="col-md-12">
                        <label for="images" class="form-label">Book Image :</label>
                        <input type="file" class="form-group" name="images">
                        <br>
                        <img src="{{ asset('Uploads/'.$dt->images) }}" widht="300px"/>
                        <br>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </form>
                @endforeach
            </div>

Here, I will update the data, if user want to change the images, so the image will be update and if no, the images use the old one. It's rull same as the other input. But, when I try to upload the new one, the error Call to a member function getClientOriginalName() happen. Do you have any suggestion to this? I already add the code enctype="multipart/form-data" and var_dump() but the error was same. Thank you :)

CodePudding user response:

You have to check, with a condition, if your image is null or not in order to avoid this error.

Try something like :

if ( isset($request->file('images')) != null ) {
    $ChecK = 0;
    $file = $request->file('images')->getClientOriginalName();
    $filename = time().$file;
    $path = public_path('/Uploads');
}
//..

CodePudding user response:

Just add a simple condition :

If($request->file('image')) {
  ...
$request->images->getClientOriginalNe();
.....

}
  • Related