Laravel 4: how to run a raw SQL?

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

    The best way to do this I have found so far it to side step Laravel and execute the query directly using the Pdo object.

    Example

    DB::connection()->getPdo()->exec( $sql );
    

    I usually find it faster and more efficient for a one time query to simply open my database query tool and type the query with full syntax checking then execute it directly.

    This becomes essential if you have to work with stored procedures or need to use any database functions

    Example 2 setting created_at to a the value you need it to be and side steeping any carbon funkiness

    $sql = 'UPDATE my_table SET updated_at = FROM_UNIXTIME(nonce) WHERE id = ' . strval($this->id);
    DB::statement($sql);
    

    I found this worked in a controller but not in a migration

提交回复
热议问题