Laravel 4: how to run a raw SQL?

前端 未结 7 978
情书的邮戳
情书的邮戳 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:39

    In the Laravel 4 manual - it talks about doing raw commands like this:

    DB::select(DB::raw('RENAME TABLE photos TO images'));
    

    edit: I just found this in the Laravel 4 documentation which is probably better:

    DB::statement('drop table users');
    

    Update: In Laravel 4.1 (maybe 4.0 - I'm not sure) - you can also do this for a raw Where query:

    $users = User::whereRaw('age > ? and votes = 100', array(25))->get();
    

    Further Update If you are specifically looking to do a table rename - there is a schema command for that - see Mike's answer below for that.

提交回复
热议问题