Raw SQL Queries in Laravel

Add these to your routes to see raw SQL working:

Route::get('/insert', function () {
    DB::insert('insert into posts(title, content) values(?,?)',['PHP with Laravel', 'Laravel is the best thing that has happened to PHP']);
});

Route::get('/read', function () {
    $results = DB::select('select * from posts where id=?',[1]);
    foreach($results as $post){
        return $post->title;
    }
});

Route::get('/update', function () {     
    $updated = DB::update('update posts set title="Update title" where id=?',[1]);
    return $updated;
});  

Route::get('/delete', function () {     
    $deleted = DB::delete('delete from posts where id=?',[1]);
    return $deleted;
});  

Leave a Reply

Your email address will not be published. Required fields are marked *