Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Laravel MySql RAW Update, Delete or Select

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.46k
    Comment on it

    Hi All,

    Recently I needed to update, delete and select using RAW query in Laravel 5.0. When I searched the internet the only query I was able to find out is the select query but I need to accomplish both update and delete also.So after little more R&D I came to know I can use the same query to accomplish that with very little change.

    To demonstrate to you how we can achieve that I have written down the syntax of all.

    Syntax:-


    1) Select :- To select records from a table.

    $response = DB::select(DB::raw("SELECT * FROM userinfo"));
    

    2) Update :- To update a records of a table.

    $finalResult = DB::update(DB::raw("UPDATE {$tableName} SET comment='{$correctData['comment']}', modified='{$correctData['modified']}' {$updateCondition}"));
    

    3) Delete :- To delete a records of a table.

    $finalResult = DB::delete(DB::raw("DELETE FROM {$tableName} WHERE {$deleteCondition}"));
    
    //To chect if the query executed properly you may use the following syntax 
    if($finalResult == 1){
       //SUCCESS
    } else {
       //ERROR
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: