关闭

laravel Model模型

1178人阅读 评论(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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:216412次
    • 积分:3293
    • 等级:
    • 排名:第10285名
    • 原创:153篇
    • 转载:66篇
    • 译文:0篇
    • 评论:13条
    最新评论