helpmyworld's avatar

Call to a member function move() on null

I suspect that the solution may be obvious but I Am not winning. Please assist to solve this issue. I want to save a book with image(image works well) and pdf (which is not uploading because of the error)

How can save/store pdf and image at the same time?

 public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'description' => 'required',
            'price' => 'required',
            'image' => 'required|image',
            'file_name' => 'required',
        ]);
        $book = new Book;
        // Upload Image
        if($request->hasFile('image')){
            $image_tmp = Input::file('image');
            if ($image_tmp->isValid()) {
                // Upload Images after Resize
                $extension = $image_tmp->getClientOriginalExtension();
                $book_image_new_name = rand(111,99999).'.'.$extension;
                $image = 'uploads/books'.'/'.$book_image_new_name;
                Image::make($image_tmp)->save($image);
                $book->image = $book_image_new_name;
            }
        }
        //Upload i want to upload a pdf please help
        $file = $request->file('pdf');
        $destinationPath = 'uploads/books';
        $file->move($destinationPath,$file->getClientOriginalName()); // this line here is the problem or throws an error

        $book->name = $request->name;
        $book->description = $request->description;
        $book->price = $request->price;
        $book->pages = $request->pages;
        $book->author = $request->author;
        $book->isbn = $request->isbn;
        $book->publisher = $request->publisher;
        $book = $request->file('pdf');
        $book->image = 'uploads/books/' . $book_image_new_name;
        $book->save();
        Session::flash('success', 'Book created.');

        return redirect()->back()->with('msg','Book added successfully');

    }
0 likes
3 replies
Norbertho's avatar

dd($request->all());

do this before the validation and let us know what you see(screeenshot)

helpmyworld's avatar

these are my results after dd

array:10 [▼
  "_token" => "w9VYp7T8VOHLE46rUfDGvkbGtpj1I5Vsld4ThD7z"
  "name" => "Daily Journal"
  "pages" => "14"
  "publisher" => "Helpmyworld"
  "author" => "rorisang Maimane"
  "isbn" => "9781920488772"
  "price" => "280"
  "description" => "<p>zdfsdfdsdfsdfddfs</p>"
  "image" => Illuminate\Http\UploadedFile {#621 ▶}
  "file_name" => Illuminate\Http\UploadedFile {#615 ▶}
]
yibr's avatar

try this

 $file->move($destinationPath, $request->file('pdf'));

Please or to participate in this conversation.