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

查看:15
本文介绍了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.如果有 Eloquent 解决方案,我也想知道如何运行原始 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天全站免登陆