Laravel 4:如何运行原始SQL? [英] Laravel 4: how to run a raw SQL?

查看:110
本文介绍了Laravel 4:如何运行原始SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel 4中重命名表,但不知道该怎么做.

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

SQL是alter table photos rename to images.如果有一个雄辩的解决方案,我也想知道如何运行原始SQL,因为有时别无选择.

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.

推荐答案

Laravel 4手册-讨论执行这样的原始命令:

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

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

编辑:我刚刚在 Laravel 4文档中找到了这个可能更好:

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

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

更新:在Laravel 4.1(也许是4.0-我不确定)中,您也可以针对原始的Where查询执行此操作:

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();

进一步更新:如果您专门想对表进行重命名-有一个用于该表的模式命令-有关此问题,请参见下面的Mike回答.

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.

这篇关于Laravel 4:如何运行原始SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆