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

c3422867's avatar

Raw SQL query and binding data?

create.blade.php

@csrf

First Name Last Name SAVE

controller >

Route::POST('/form-filled', 'PagesController@store');

& then my store function is:

DB::insert('insert into projects (title, description) values (?, ?)', ['Dynamic' 'some stuff about stuff']);

I can get my form values as follows in my store function:

$title = request('title'); $description = request('description');

How do I use the $title and $description variables into the query?

0 likes
6 replies
c3422867's avatar

Basically want to know how to add variable to the raw insert query instead of specifying values?

tisuchi's avatar
tisuchi
Best Answer
Level 70

@c3422867

What if you try this?

public function store(Request $request){
    $title = $request->title;
    $description = $request->description;

    \DB::insert('insert into projects (title, description) values (?, ?)', [$title, $description]);    
}
2 likes
c3422867's avatar

How do I update?

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

c3422867's avatar

Nevermind, I figured it out, just posting as may help someone else.

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

GamerFac3's avatar

Why exactly aren't you using Eloquent for that? There'S really no need to write the sql yourself with laravel. Hope you noticed that in the last month, since you started the discussion.

Please or to participate in this conversation.