laravel Model模型

11 篇文章 0 订阅


php artisan make:model Article

php artisan make:model Page

root@cy-VirtualBox:/var/www/html/laravel5.0# php artisan make:model Article

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0

PHP Warning:  Module 'json' already loaded in Unknown on line 0

PHP Warning:  Module 'phalcon' already loaded in Unknown on line 0

Model created successfully.

Created Migration: 2015_07_08_145606_create_articles_table

root@cy-VirtualBox:/var/www/html/laravel5.0# 



root@cy-VirtualBox:/var/www/html/laravel5.0# php artisan make:model Page

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0

PHP Warning:  Module 'json' already loaded in Unknown on line 0

PHP Warning:  Module 'phalcon' already loaded in Unknown on line 0

Model created successfully.

Created Migration: 2015_07_08_145720_create_pages_table

root@cy-VirtualBox:/var/www/html/laravel5.0# 



接下来进行 Article 和 Page 类对应的 articles 表和 pages表的数据库迁移,进入 `learnlaravel5/database/migrations` 文件夹。

在 ***_create_articles_table.php 中修改:

Schema::create('articles', function(Blueprint $table)
{
	$table->increments('id');
	$table->string('title');
	$table->string('slug')->nullable();
	$table->text('body')->nullable();
	$table->string('image')->nullable();
	$table->integer('user_id');
	$table->timestamps();
});

在 ***_create_pages_table.php 中修改:

Schema::create('pages', function(Blueprint $table)
{
	$table->increments('id');
	$table->string('title');
	$table->string('slug')->nullable();
	$table->text('body')->nullable();
	$table->integer('user_id');
	$table->timestamps();
});

然后执行命令:

php artisan migrate


root@cy-VirtualBox:/var/www/html/laravel5.0# php artisan migrate


成功以后, tables 表和 pages 表已经出现在了数据库里


root@cy-VirtualBox:~# mysql -h localhost -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 53

Server version: 5.5.43-0ubuntu0.14.04.1 (Ubuntu)


Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> use laravel5

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> show tables;

+--------------------+

| Tables_in_laravel5 |

+--------------------+

| articles           |

| migrations         |

| pages              |

| password_resets    |

| users              |

+--------------------+

5 rows in set (0.00 sec)


mysql> 



数据库填充 Seeder:


在 `learnlaravel5/database/seeds/` 下新建 `PageTableSeeder.php` 文件,内容如下:

<?php

use Illuminate\Database\Seeder;
use App\Page;

class PageTableSeeder extends Seeder {

  public function run()
  {
    DB::table('pages')->delete();

    for ($i=0; $i < 10; $i++) {
      Page::create([
        'title'   => 'Title '.$i,
        'slug'    => 'first-page',
        'body'    => 'Body '.$i,
        'user_id' => 1,
      ]);
    }
  }

}

然后修改同一级目录下的 `DatabaseSeeder.php`中:

// $this->call('UserTableSeeder');

这一句为

$this->call('PageTableSeeder');

然后运行命令进行数据填充:

composer dump-autoload

php artisan db:seed

ot@cy-VirtualBox:/var/www/html/laravel5.0# composer dump-autoload

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0

PHP Warning:  Module 'json' already loaded in Unknown on line 0

PHP Warning:  Module 'phalcon' already loaded in Unknown on line 0

Generating autoload files

root@cy-VirtualBox:/var/www/html/laravel5.0# php artisan db:seed

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0

PHP Warning:  Module 'json' already loaded in Unknown on line 0

PHP Warning:  Module 'phalcon' already loaded in Unknown on line 0

Seeded: PageTableSeeder

root@cy-VirtualBox:/var/www/html/laravel5.0# 


去看看 pages 表


root@cy-VirtualBox:~# mysql -h localhost -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 54

Server version: 5.5.43-0ubuntu0.14.04.1 (Ubuntu)


Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> use laravel5;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> show tables

    -> ;

+--------------------+

| Tables_in_laravel5 |

+--------------------+

| articles           |

| migrations         |

| pages              |

| password_resets    |

| users              |

+--------------------+

5 rows in set (0.00 sec)


mysql> select * from pages;

Empty set (0.00 sec)


mysql> select * from pages;

+----+---------+------------+--------+---------+---------------------+---------------------+

| id | title   | slug       | body   | user_id | created_at          | updated_at          |

+----+---------+------------+--------+---------+---------------------+---------------------+

|  1 | Title 0 | first-page | Body 0 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  2 | Title 1 | first-page | Body 1 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  3 | Title 2 | first-page | Body 2 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  4 | Title 3 | first-page | Body 3 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  5 | Title 4 | first-page | Body 4 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  6 | Title 5 | first-page | Body 5 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  7 | Title 6 | first-page | Body 6 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  8 | Title 7 | first-page | Body 7 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

|  9 | Title 8 | first-page | Body 8 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

| 10 | Title 9 | first-page | Body 9 |       1 | 2015-07-08 15:16:41 | 2015-07-08 15:16:41 |

+----+---------+------------+--------+---------+---------------------+---------------------+

10 rows in set (0.09 sec)


mysql> 




参考:http://lvwenhan.com/laravel/432.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值