skip to Main Content

I have a CRUD system specifically for updates, I have tried to update the data and all the data is updated correctly except the images.

this is my controller code:

public function update_book(Request $request, $id)
    {
        // dd($request->all());
        
        $book = Book::find($id);

        $imageName = $request->file('image')->store('public/book');

        $book->update([
            'name' => $request->name,
            'date' => $request->date,
            'author' => $request->author,
            'stock' => $request->stock,
            'description' => $request->description,
            'image' => $imageName
        ]);

        $book->categories()->sync($request->categories);

        return redirect()->route('admin.books_panel')->with('success', 'Data Successfully Updated!');
    }

this is my react code:

    const [image, setImage] = useState<File | null>(null)
    const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        if (e.target.files && e.target.files[0]) {
          setImage(e.target.files[0])
        }
    }

    const updateBook = async (e: FormEvent<HTMLFormElement>) => {
        e.preventDefault()
        
        let cat: string[] = []
        selectedCategories.forEach(categoryId => {
            cat.push(categoryId)
        })
        
        Inertia.put(`/admin/books-panel/${book.id}`, {
            name: name,
            date: date,
            author: author,
            stock: stock,
            description: description,
            image: image as Blob,
            categories: cat.slice(1)
        })
    }

dd result:

array:7 [▼ // appHttpControllersAdminController.php:114
  "name" => "ddd"
  "date" => "2024-03-05"
  "author" => "ccc"
  "stock" => 1111
  "description" => "ccc"
  "image" => null
  "categories" => array:2 [▶]
]

some tutorial recommend me to use formData.append, i’ll already tried it and the result is 0% working

2

Answers


  1. In your controller try using this method

    public function update_book(Request $request, $id)
        {
            // dd($request->all());
            
            $book = Book::find($id);
    
            // $imageName = $request->file('image')->store('public/book');
            $imageName = $request->image->getClientOriginalName();
            $request->image->move(public_path('book/'),$imageName);
    
            $book->update([
                'name' => $request->name,
                'date' => $request->date,
                'author' => $request->author,
                'stock' => $request->stock,
                'description' => $request->description,
                'image' => $imageName
            ]);
    
            $book->categories()->sync($request->categories);
    
            return redirect()->route('admin.books_panel')->with('success', 'Data Successfully Updated!');
        }
    
    Login or Signup to reply.
  2. I think there is an issue with you processing the file and saving at the end. Also you can’t dd() the files uploaded with $request->all() .

    You should instead try this:

    if ($request->hasFile('image')) {
            $imageName = $request->file('image');
            // Save the image to storage or perform other operations
    }
    

    And if you really want to dd() the file input, try:

    dd($request->file('image');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search