Dowemo


Question:

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, I'd also like to know how to run a raw SQL, cause sometimes there's just no alternative.


Best Answer:


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





Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs