如何查看 Laravel 5 的所有laravel数据库迁移请求

今天看啥 热点:
Laravel 5框架学习之数据库迁移(Migrations),laravelmigrationsdatabase migrations 是laravel最强大的功能之一。数据库迁移可以理解为数据库的版本控制器。
在 database/migrations 目录中包含两个迁移文件,一个建立用户表,一个用于用户密码重置。
在迁移文件中,up 方法用于创建数据表,down方法用于回滚,也就是删除数据表。
执行数据库迁移
复制代码 代码如下:
php artisan migrate
Migration table created successfully.
Migrated: _000000_create_users_table
Migrated: _100000_create_password_resets_table
查看mysql数据库,可以看到产生了三张表。 migratoins 表是迁移记录表,users 和 pasword_resets。
如果设计有问题,执行数据库回滚
复制代码 代码如下:
php artisan migrate:rollback
Rolled back: _100000_create_password_resets_table
Rolled back: _000000_create_users_table
再次查看mysql数据库,就剩下 migrations 表了, users password_resets 被删除了。
修改迁移文件,再次执行迁移。
复制代码 代码如下:
php artisan make:migration create_article_table --create='articles'
Created Migration: _050138_create_article_table
在 database/migrations 下生成了新的文件。
use Illuminate\Database\Schema\B
use Illuminate\Database\Migrations\M
class CreateArticleTable extends Migration {
* Run the migrations.
* @return void
public function up()
Schema::create('articles', function(Blueprint $table)
$table-&increments('id');
$table-&timestamps();
* Reverse the migrations.
* @return void
public function down()
Schema::drop('articles');
自动添加了 id列,自动增长,timestamps() 会自动产生 created_at 和 updated_at 两个时间列。我们添加一些字段:
public function up()
Schema::create('articles', function(Blueprint $table)
$table-&increments('id');
$table-&string('title');
$table-&text('body');
$table-&timestamp('published_at');
$table-&timestamps();
执行迁移:
复制代码 代码如下:
php artisan migrate
现在有了新的数据表了。
假设我们需要添加一个新的字段,你可以回滚,然后修改迁移文件,再次执行迁移,或者可以直接新建一个迁移文件
复制代码 代码如下:
php artisan make:migration add_excerpt_to_articels_table
查看新产生的迁移文件
use Illuminate\Database\Schema\B
use Illuminate\Database\Migrations\M
class AddExcerptToArticelsTable extends Migration {
* Run the migrations.
* @return void
public function up()
* Reverse the migrations.
* @return void
public function down()
只有空的 up 和 down 方法。我们可以手工添加代码,或者我们让laravel为我们生成基础代码。删除这个文件,重新生成迁移文件,注意添加参数:
复制代码 代码如下:
php artisan make:migration add_excerpt_to_articels_table --table='articles'
现在,up 方法里面有了初始代码。
public function up()
Schema::table('articles', function(Blueprint $table)
添加实际的数据修改代码:
public function up()
Schema::table('articles', function(Blueprint $table)
$table-&text('excerpt')-&nullable();
public function down()
Schema::table('articles', function(Blueprint $table)
$table-&dropColumn('excerpt');
nullable() 表示字段也可以为空。
再次执行迁移并检查数据库。
如果我们为了好玩,执行回滚
复制代码 代码如下:
php artisan migrate:rollback
excerpt 列没有了。
以上所述就是本文的全部内容了,希望能够给大家熟练掌握Laravel5框架有所帮助。
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&
PHP教程最近更新在Laravel中执行数据库操作有两种方式,一种是使用DB外观对象的静态方法直接执行sql查询,另外一种是使用Model类的静态方法(实际上也是Facade的实现,使用静态访问方式访问Model的方法,内部采用了__callStatic魔术方法代理了对成员方法的访问。查询操作基本查询操作使用sql语句执行select查询操作$results = DB::select(&select * from users where id = ?&, [1]);foreach ($results as $res) {
echo $res->}返回结果为数组,数组中每一个值为一个StdClass对象。也可以使用命名绑定,推荐使用这种方式,更加清晰一些$results = DB::select(&select * from users where id = :id&, [&id& => 1]);从数据表中取得所有的数据列$users = DB::table(&users&)->get();foreach ($users as $user){
var_dump($user->name);}从表中查询单行/列使用first方法返回单行数据,该方法返回的是一个stdClass对象$user = DB::table(&users&)->where(&name&, &John&)->first();echo $user->如果只需要一列的值,则可以使用value方法直接获取单列的值$email = DB::table(&users&)->where(&name&, &John&)->value(&email&);从数据表中分块查找数据列该方法用于数据表中有大量的数据的操作,每次从结果集中取出一部分,使用闭包函数进行处理,然后再处理下一部分,该命令一般用于Artisan命令行程序中处理大量数据。DB::table(&users&)->chunk(100, function($users){
foreach ($users as $user)
}});在闭包函数中,如果返回false,则会停止后续的处理。从数据表中查询某一列的列表比如我们希望查询出角色表中所有的title字段值$titles = DB::table(&roles&)->pluck(&title&);foreach ($titles as $title) {
echo $}这里的pluck函数有两个参数Collection pluck( string $column, string|null $key = null)第一个参数为要查询的列,第二个参数是每一列的key$roles = DB::table(&roles&)->pluck(&title&, &name&);foreach ($roles as $name => $title) {
echo $}聚集函数查询构造器也提供了一些聚集函数如count,max,min,avg,sum等$users = DB::table(&users&)->count();$price = DB::table(&orders&)->max(&price&);$price = DB::table(&orders&)
->where(&finalized&, 1)
->avg(&price&);指定select查询条件查询指定的列$users = DB::table(&users&)->select(&name&, &email as user_email&)->get();如果已经指定了select,但是又希望再次添加一些字段,使用它addSelect方法$query = DB::table(&users&)->select(&name&);$users = $query->addSelect(&age&)->get();查询不同的结果distinct$users = DB::table(&users&)->distinct()->get();使用原生表达式使用DB::raw方法可以向查询中注入需要的sql片段,但是非常不推荐使用该方法,用不好会 产生sql注入$users = DB::table(&users&)
->select(DB::raw(&count(*) as user_count, status&))
->where(&status&, &&, 1)
->groupBy(&status&)
->get();Join操作内连接 Inner Join使用join执行内连接操作,该函数第一个参数为要连接的表名,其它参数指定了连接约束$users = DB::table(&users&)
->join(&contacts&, &users.id&, &=&, &contacts.user_id&)
->join(&orders&, &users.id&, &=&, &orders.user_id&)
->select(&users.*&, &contacts.phone&, &orders.price&)
->get();左连接 Left Join使用leftJoin方法执行左连接操作,参数和join一样$users = DB::table(&users&)
->leftJoin(&posts&, &users.id&, &=&, &posts.user_id&)
高级Join方法如果join方法的约束条件比较复杂,可以使用闭包函数的方式指定DB::table(&users&)
->join(&contacts&, function ($join) {
$join->on(&users.id&, &=&, &contacts.user_id&)->orOn(...);
->get();如果join约束中要使用列值与指定数组比较,则可以使用where和OrWhere方法DB::table(&users&)
->join(&contacts&, function ($join) {
$join->on(&users.id&, &=&, &contacts.user_id&)
->where(&contacts.user_id&, &>&, 5);
->get();Union操作要使用union操作,可以先创建一个query,然后再使用union方法去绑定第二个query$first = DB::table(&users&)
->whereNull(&first_name&);$users = DB::table(&users&)
->whereNull(&last_name&)
->union($first)
->get();同样,unionAll方法也是可以使用的,参数与union相同。Where查询条件简单的wehere条件使用where方法为查询增加where条件,该函数一般需要三个参数:列名,操作符(任何数据库支持的操作符都可以),列值。$users = DB::table(&users&)->where(&votes&, &=&, 100)->get();$users = DB::table(&users&)->where(&votes&, 100)->get();为了方便起见,如果只提供两个参数,则默认第二个参数为=,执行相等匹配。$users = DB::table(&users&)
->where(&votes&, &>=&, 100)
->get();$users = DB::table(&users&)
->where(&votes&, &&, 100)
->get();$users = DB::table(&users&)
->where(&name&, &like&, &T%&)
->get();where条件也可以使用数组提供:$users = DB::table(&users&)->where([
[&status&,&1&],
[&subscribed&,&&,&1&],])->get();OR条件如果where条件要使用or操作,则使用orWhere方法$users = DB::table(&users&)
->where(&votes&, &>&, 100)
->orWhere(&name&, &John&)
->get();其它where条件whereBetween / whereNotBetween$users = DB::table(&users&)
->whereBetween(&votes&, [1, 100])->get();$users = DB::table(&users&)
->whereNotBetween(&votes&, [1, 100])->get();whereIn / whereNotIn$users = DB::table(&users&)
->whereIn(&id&, [1, 2, 3])
->get();$users = DB::table(&users&)
->whereNotIn(&id&, [1, 2, 3])
->get();whereNull / whereNotNull$users = DB::table(&users&)
->whereNull(&updated_at&)
->get();$users = DB::table(&users&)
->whereNotNull(&updated_at&)
->get();高级where条件参数组(嵌套条件)DB::table(&users&)
->where(&name&, &=&, &John&)
->orWhere(function ($query) {
$query->where(&votes&, &>&, 100)
->where(&title&, &&, &Admin&);
->get();上述代码等价于下列sqlselect * from users where name = &John& or (votes > 100 and title
&Admin&)whereExists (where exist)DB::table(&users&)
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from(&orders&)
->whereRaw(&orders.user_id = users.id&);
->get();上述代码与下列sql等价select * from userswhere exists (
select 1 from orders where orders.user_id = users.id)JSON类型的列查询MySQL 5.7和Postgres数据库中提供了新的数据类型json,对json提供了原生的支持,使用->可以对json列进行查询。$users = DB::table(&users&)
->where(&options->language&, &en&)
->get();$users = DB::table(&users&)
->where(&preferences->dining->meal&, &salad&)
->get();Ordering, Grouping, Limit, & Offset$users = DB::table(&users&)
->orderBy(&name&, &desc&)
$users = DB::table(&users&)
->groupBy(&account_id&)
->having(&account_id&, &>&, 100)
$users = DB::table(&orders&)
->select(&department&, DB::raw(&SUM(price) as total_sales&))
->groupBy(&department&)
->havingRaw(&SUM(price) > 2500&)
->get();要限制查询返回的结果行数,或者是跳过指定行数的结果(OFFSET),可以使用skip和take方法$users = DB::table(&users&)->skip(10)->take(5)->get();插入操作使用sql语句执行插入插入操作与select操作类似,使用insert函数DB::insert(&insert into users (id, name) values (?, ?)&, [1, &Dayle&]);基本插入操作DB::table(&users&)->insert(
[&email& => &[email protected]&, &votes& => 0]);DB::table(&users&)->insert([
[&email& => &[email protected]&, &votes& => 0],
[&email& => &[email protected]&, &votes& => 0]]);如果希望插入后能够获取新增数据的id,则可以使用insertGetId方法$id = DB::table(&users&)->insertGetId(
[&email& => &[email protected]&, &votes& => 0]);更新操作使用sql语句执行更新操作执行DB中的update后,会返回 操作影响的数据行数DB::update(&update users set votes = 100 where name = ?&, [&John&]);基本更新操作DB::table(&users&)
->where(&id&, 1)
->update([&votes& => 1]);指定列的增减DB::table(&users&)->increment(&votes&);DB::table(&users&)->increment(&votes&, 5);DB::table(&users&)->decrement(&votes&);DB::table(&users&)->decrement(&votes&, 5);在执行自增/减操作的时候,也可以同时更新其它列DB::table(&users&)->increment(&votes&, 1, [&name& => &John&]);删除操作使用sql执行删除执行DB中的delete后,会返回 操作影响的数据行数DB::delete(&delete from users&);基本删除操作DB::table(&users&)->delete();DB::table(&users&)->where(&votes&, &delete();如果希望truncate整个表,则使用truncate方法DB::table(&users&)->truncate();悲观锁使用sharedLock方法可以避免选定的行在事务提交之前被修改DB::table(&users&)->where(&votes&, &>&, 100)->sharedLock()->get();另外lockForUpdate方法可以避免其它的共享锁修改或者是选定DB::table(&users&)->where(&votes&, &>&, 100)->lockForUpdate()->get();事务处理使用transaction方法的callback函数执行事务处理DB::transaction(function(){
DB::table(&users&)->update([&votes& => 1]);
DB::table(&posts&)->delete();});在回调函数中,抛出任何异常都会导致事务回滚如果需要手动管理事务,则使用如下函数DB::beginTransaction();DB::rollback();DB::commit();使用DB类的静态方法启用的事务不仅对普通sql查询有效,对Eloquent ORM同样有效,因为它内部也是调用了DB类的数据库连接。查看日志记录查看请求执行的sql日志记录,需要先执行enableQueryLog开启,然后执行getQueryLog获取DB::connection()->enableQueryLog();$queries = DB::getQueryLog();其它操作执行一般的sql语法DB::statement(&drop table users&);监听查找事件,可以用来对执行的sql进行记录DB::listen(function($sql, $bindings, $time){
// $query->sql
// $query->bindings
// $query->time});获取某个数据库连接$users = DB::connection(&foo&)->select(...);如果还不能满足需求,可以获取PDO对象$pdo = DB::connection()->getPdo();这样不管什么操作都可以做了吧另外含有两个方法,用于重新连接到指定数据库和断开连接DB::reconnect(&foo&);DB::disconnect(&foo&)d;参考: Laravel 5.2 官方文档内容来源:/a/2605
HTML &button& 和 input typ
最新教程周点击榜
微信扫一扫我们可以使用DB::insert()方法插入记录。insert()方法的语法如下表中所示。
bool insert(string $query, array $bindings = array())
$query(string) –&在查询数据库中执行
$bindings(array) –&与查询绑定值
在数据库上运行 INSERT 语句
第1步-&执行以下命令来创建一个名为-&StudInsertController&的控制器
php artisan make:controller StudInsertController
第2步&-&成功执行后,您会收到以下输出&-
第3步&-&将以下代码复制到文件 -&app/Http/Controllers/StudInsertController.php
namespace App\Http\C
use Illuminate\Http\R
use App\Http\R
use App\Http\Controllers\C
class StudInsertController extends Controller {
public function insertform(){
return view('stud_create');
public function insert(Request $request){
$name = $request-&input('stud_name');
DB::insert('insert into student (name,age) values(?,?)',[$name, $age]);
echo "Record inserted successfully.&br/&";
echo '&a href = "/insert"&Click Here&/a& to go back.';
第4步&-&创建一个名为&&resources/views/stud_create.php 的视图文件,并复制下面的代码到此文件中。
resources/views/stud_create.php
&title&添加 - 学生管理&/title&
&form action = "/create" method = "post"&
&input type = "hidden" name = "_token" value = "&?php echo csrf_token(); ?&"&
&td&名字:&/td&
&td&&input type='text' name='stud_name' /&&/td&
&td&年龄:&/td&
&td&&input type='text' name='stud_age' /&&/td&
&td colspan = '2'&
&input type = 'submit' value = "添加学生"/&
第5步&-&添加以下行到&app/Http/routes.php&文件。
app/Http/routes.php
Route::get('insert','StudInsertController@insertform');
Route::post('create','StudInsertController@insert');
第6步&-&请访问以下网址以将数据记录插入数据库中。
http://localhost:8000/insert
第7步&- 结果显示如下图所示。
点击“添加学生”后,结果如下图所示:
数据库 student 表中的记录如下:
本站代码下载:Laravel5异步队列使用方法_服务器应用_Linux公社-Linux系统门户网站
你好,游客
Laravel5异步队列使用方法
来源:Linux社区&
作者:Linux
Laravel5提供了完善的队列功能,只需要简单的配置即可使用。这里简单记录下关于异步队列的用法。
关于队列的定义,这里就不作介绍了。我们要使用异步队列就有两个关键:
1.存储队列的地方
2.执行任务的服务
打开 config/queue.php ,这是Laravel5关于队列的配置文件。首先我们可以通过 default 参数指定默认队列驱动,默认配置是 sync , 这是同步队列,我们要做异步队列首先就要改变这里。假设我们用 database 作为驱动,队列任务将会存放在数据库中,而我们后面会另外启动一个后台服务来处理队列任务,这就是异步方式了。
'default' =& 'database'
修改完配置后,我们需要创建一个表来存放队列任务,Laravel5已经在自带artisan命令中内置了一个指令用来生成数据迁移,只需要两条命令即可,当然你得实现配置好数据库连接。
php artisan queue:
php artisan migrate
这样就自动在数据库中创建了 jobs 表。
启动队列监听服务
通过下面这条指令启动队列监听服务,它会自动处理 jobs 表中的队列任务:
php artisan queue:listen
在linux中,如果想让它在后台执行,可以这样:
nohup php artisan queue:listen &
添加队列任务
关于队列任务的添加,手册里说的比较详细,这里就简单举个例子吧。
首先,通过artisan创建一个队列命令:
php artisan make:command SendEmail --queued
这样会生成 app/Commands/SendEmail.php 这个类文件,这个类会被标识为队列命令,你可以在 handle 方法中写自己的业务逻辑。
在控制器中,可以简单通过 Bus::dispatch 分发任务:
Bus::dispatch(new \App\Commands\SendEmail());
你会发现任务不会立即执行,而是被放到 jobs 表中,由队列监听服务处理。
更详细的用法建议参考 command bus 和 queue 相关的手册章节。
本文永久更新链接地址:
相关资讯 & & &
& (03月15日)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款

我要回帖

更多关于 laravel5.3 查看sql 的文章

 

随机推荐