Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

c3422867's avatar

DB::update query

I am trying to use the query using the valided values, $book_name, $isbn_no & $book_price and ofcourse the $Id but the query doesn't work.

public function update(Request $request, $id) {

      $validatedData = $request->validate([
        'book_name' => 'required|max:255',
        'isbn_no' => 'required|alpha_num',
        'book_price' => 'required|numeric',
    ]);

$book_name = $validatedData['book_name']; $isbn_no = $validatedData['isbn_no']; $book_price = $validatedData['book_price'];

DB::update('update Books (book_name, isbn_no, book_price) value (?,?,?), [$book_name, $isbn_no, $book_price] where id = $id');

return redirect('/books')->with('success', 'Book is successfully updated'); }

0 likes
11 replies
c3422867's avatar

The DB::update query needs sorting so that I can update the values where id = $id;

Nakov's avatar

What about:

DB::table('Books')->where('id', $id)->update($validatedData);
c3422867's avatar

Thanks, but I dont want to do it that way, I want to get better at SQL querying so don't want to use eloquent.

Nakov's avatar

I use the query builder above, not Eloquent. Eloquent is if you use models.

Nakov's avatar

@c3422867 you can use this then:

DB::update('update Books (book_name, isbn_no, book_price) value (?,?,?) where id = ?', [$book_name, $isbn_no, $book_price, $id]);

This is not a good way to learn SQL btw :)

jlrdw's avatar

If you are new also to sql see: http://www.mysqltutorial.org/

See the free videos here on laracasts

Work examples in the docs.

Try first, if stuck post a question with code.

Nakov's avatar

@jlrdw yup, good for him.

@c3422867 use a tool like MySQL workbench, or any tool for Database, but not through a framework.

c3422867's avatar

haha thanks, I saw something on stackoverflow, figured it out and then came back to post by which time @nakov provided his answer which is writtens slightly differently to my one but both work the same :)

Please or to participate in this conversation.