Laravel 4: how to run a raw SQL?

前端 未结 7 970
情书的邮戳
情书的邮戳 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 01:40

    The accepted way to rename a table in Laravel 4 is to use the Schema builder. So you would want to do:

    Schema::rename('photos', 'images');
    

    From http://laravel.com/docs/4.2/schema#creating-and-dropping-tables

    If you really want to write out a raw SQL query yourself, you can always do:

    DB::statement('alter table photos rename to images');
    

    Note: Laravel's DB class also supports running raw SQL select, insert, update, and delete queries, like:

    $users = DB::select('select id, name from users');
    

    For more info, see http://laravel.com/docs/4.2/database#running-queries.

提交回复
热议问题