Laravel 4: how to run a raw SQL?

前端 未结 7 946
情书的邮戳
情书的邮戳 2020-11-29 01:26

I want to rename a table in Laravel 4, but don\'t know how to do that.

The SQL is alter table photos rename to images. If there is an Eloquent solution,

相关标签:
7条回答
  • 2020-11-29 02:03

    Laravel raw sql – Insert query:

    lets create a get link to insert data which is accessible through url . so our link name is ‘insertintodb’ and inside that function we use db class . db class helps us to interact with database . we us db class static function insert . Inside insert function we will write our PDO query to insert data in database . in below query we will insert ‘ my title ‘ and ‘my content’ as data in posts table .

    put below code in your web.php file inside routes directory :

    Route::get('/insertintodb',function(){
    DB::insert('insert into posts(title,content) values (?,?)',['my title','my content']);
    });
    

    Now fire above insert query from browser link below :

    localhost/yourprojectname/insertintodb
    

    You can see output of above insert query by going into your database table .you will find a record with id 1 .

    Laravel raw sql – Read query :

    Now , lets create a get link to read data , which is accessible through url . so our link name is ‘readfromdb’. we us db class static function read . Inside read function we will write our PDO query to read data from database . in below query we will read data of id ‘1’ from posts table .

    put below code in your web.php file inside routes directory :

    Route::get('/readfromdb',function() {
        $result =  DB::select('select * from posts where id = ?', [1]);
        var_dump($result);
    });
    

    now fire above read query from browser link below :

    localhost/yourprojectname/readfromdb
    
    0 讨论(0)
提交回复
热议问题