手把手教你跑Larave框架 实战笔记系列之三

作者: 何朱必 分类: 开发体验笔记 发布时间: 2018-06-19 14:03 ė views 6 23条评论

系列之二关联上了MVC(Model-View-Controller)架构各个分支,已经初步了解MVC的一般流程是这样的:View(界面)触发事件→Controller(业务)处理了业务,然后触发了数据更新→不知道谁更新了Model的数据→Model(带着数据)回到了View→View更新数据。今天就重点来扯一下Model数据模型这个重量级的话题……
PHP的威力在于动态网页设计,所谓动态网页设计就是前台静态页面(.htm)关联后台动态页面(.php)的机制,核心是后台有数据库通过数据模型在响应用户业务逻辑。说到这里,数据库该闪亮登场了……
细心的童鞋会发现“一键安装”好的laravel 跟正常使用的项目相比差个 .env 环境配置文件。我们需要复制项目根目录下的 .env.example 示例文件并重命名为 .env,这个是项目的示例配置文件,需要把模板内容改成自己的实际配置,也就是每个PHP项目正式开发的第一步:配置环境参数。PHP原生开发时,我们一般给童鞋准备个逻辑清晰的install.php安装程序,方便童鞋们按提示配置环境参数,但laravel的官方玩法不是这样,需要手工修改环境参数示例文件.env说起来也很简单,只需修改下面的数据库连接环境变量就行了。
APP_NAME 就是自己的项目名称比如:test
APP_URL 就是我们的项目链接比如说我的 http://test.com
本地的话就是我们配置的虚拟主机自定义的本地域名。
DB_DATABASE 就是我们的数据库名比如说: test
DB_USERNAME 数据库用户名比如说: root
DB_PASSWORD 数据库密码比如说:root
改完了 .env 环境参数文件,还有项目配置项中的一些东西需要改,
laravel 5.4 以后默认使用 utf8 字符集,utf8 主要是用来支持 emoji 表情的,如果你的本地环境中的 mysql 低于5.7.7,为了防止在以后使用的过程中报错,需要找到 config/database.php 文件中的 mysql 修改 charset、collation为utf8在config/database.php 文件中的 connections 的 prefix 可以设置每种数据库的表前缀,迁移的时候不需要为每张表指定表前缀。如果使用 5.7 以上的 mysql 数据库,还可以把 strict 改为 false 关闭严格模式,以防止报类似如下 only_full_group_by 的错误。
除了数据库还有两个关键之处“时区/语言”需要改的,打开 config/app.php 文件,把 timezone 改为 PRC,laravel 默认的时区是 UTC ,中国的时区是 PRC ,如果没有改的话,那数据库存自动生成的时间会和我们的实际相差8个小时。然后把 locale 改为 zh-CN,把语言改为中文。
mysql数据库参数修改好后,接着我们需要执行
php artisan key:generate
这个是生成 APP_KEY 并自动写入到 .env 文件中的,它是 laravel 用来加密 cookie 等的。这个很关健,每次安装laravel项目时都必须记着重新生成加密的盐。
新创建的项目无迁移文件,开始创建项目主表迁移文件,例如:
php artisan make:migration create_articles_table
articles 就是我们要创建的表名(在这里是项目主表“文章”),laravel 默认表都是复数形式。
我们运行上面的命令后,会在 database/migrations 目录生成一些迁移文件,例如:
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2018_04_17_111213_create_articles_table
Migrated: 2018_04_17_111213_create_articles_table
重点是关注一下刚创建好的迁移文件create_articles_table.php模型,laravel再优雅、再省力,也不可能自动按开发者的意思自动生成业务逻辑代码,laravel只会帮我们生成通用表操作模板,针对性细节代码要结合项目需求修改一下,例如:
文章一般是有所属的分类的,我们用 int 类型的 category_id 表示分类的id;并且用 unsigned 表示无符号,用 default 设置默认值为0,用 comment 写注释。就必须加上一行代码:
$table->integer(‘category_id’)->unsigned()->default(0)->comment(‘分类id’);
再加分别用string、text 类型增加文章标题、内容字段:
$table->string(‘title’)->comment(‘标题’);
$table->text(‘content’)->comment(‘文章内容’);
$table->timestamps(); 的作用是给表增加 created_at 和 updated_at,laravel 插入和编辑数据的时候会自动通过这两个字段记录操作的日期时间。
$table->softDeletes(); 这个方法就是为表增加一个 deleted_at ,laravel 会在删除数据的时候记录操作日期,具体到我们的文章的时候就是回收站的功能了,我们可能会删除文章,但是我们还希望能恢复删除的文章,当某篇文章的 deleted_at 为 null 的时候表示正常,当有日期的时候就表示这篇文章是在这个日期被删掉了。
这样,我们就设计了一个简易的文章表了,以后根据需要再扩展功能,进一步修改这个模型或模块(.php)
再接着我们需要执行迁移才会真正生成表,执行迁移命令:
php artisan migrate
如果出现提示:Migration table created successfully.说明已经按迁移文件生成相应表。这时候我们查看数据库会发现创建了一大堆表(与迁移文件创建的表名相同)。
如果报错的话,大多是因为 .env 中的数据库账号密码配置的有问题,使用客户端或者其他项目连下数据库检查下。
数据库表有了,但是表都是空的。这时候我们需要向数据库批量添加数据的。简单来个公式:迁移 + 填充 = sql这样就容易理解迁移和填充的作用了。
首先同样是用命令行创建填充文件:
php artisan make:seeder ArticlesTableSeeder;
这会创建一个 database/seeds/ArticlesTableSeeder.php 填充文件。里面有个 run 方法,必须利用 DB 的方法写上要填充的内容,例如:
/**
* Run the database seeds.
*
* @return void
*/public function run()
{
DB::table(‘articles’)->insert(
[

[
‘category_id’ => 4,
‘title’ => ‘文章4’,
‘content’ => ‘内容4’
],
[
‘category_id’ => 5,
‘title’ => ‘文章5’,
‘content’ => ‘内容5’
]
]
);
}
和迁移样先创建好填充文件写好填充内容,接着就是要运行填充了。
执行填充命令:
php artisan db:seed;
我们会发现 database/seeds 目录下还有个 DatabaseSeeder.php 文件;是默认就存在的,并不是我们创建的。里面也有一个 run 方法,我们可以看到里面注释了一个示例:
// $this->call(UsersTableSeeder::class);
根据这个例子了,就必须按示例修改为
$this->call(ArticlesTableSeeder::class);

说明这个默认的文件并像我们自己创建的填充文件一样写填充的内容的,它是用来定义填充文件的执行顺序的;php artisan db:seed
这个是用来生成测试数据或者其他的一些基础数据的,一般到这就没啥错误了,到这里就可以愉快的访问项目了,浏览器访问 APP_URL 链接,一般项目首页起码是不会空的。如果首页并不能访问,我们可以查看 routes/web.php 文件里面定义的路由,如果需要登录,一般可以通过查看填充文件 database/seeds/UsersTableSeeder.php 来查看初始账号密码是否正确。

本文出自何朱必博客,转载时请注明出处及相应链接。

本文永久链接: https://www.hezhubi.com/laravel.htm

0

23条评论

  1. Gladis 2019年7月24日 下午2:31 回复

    Hello, i think that i saw you visited my weblog thus i got here to go back the favor?.I’m trying to
    to find issues to improve my web site!I assume its adequate to make use of a few of your ideas!!

  2. Aretha 2019年7月23日 下午6:56 回复

    This is a topic that’s near to my heart…
    Cheers! Where are your contact details though?

  3. Jamal 2019年7月23日 上午3:02 回复

    What’s up, yes this article is actually nice
    and I have learned lot of things from it
    concerning blogging. thanks.

  4. Shanna 2019年7月22日 下午10:38 回复

    Have you ever considered publishing an e-book
    or guest authoring on other blogs? I have a blog based upon on the same subjects you discuss
    and would love to have you share some stories/information. I know my readers would appreciate your work.
    If you are even remotely interested, feel free to send me an e-mail.

  5. Paula 2019年7月22日 下午9:31 回复

    An impressive share! I’ve just forwarded this onto a friend who was conducting a little
    homework on this. And he actually bought me dinner due to the fact that
    I stumbled upon it for him… lol. So let me reword this….
    Thanks for the meal!! But yeah, thanx for spending time to talk about this topic here on your internet site.

  6. Carrol 2019年7月22日 下午8:53 回复

    My developer is trying to convince me to move to .net from
    PHP. I have always disliked the idea because
    of the costs. But he’s tryiong none the less.
    I’ve been using Movable-type on a variety of websites for about a year and am concerned about switching to another platform.
    I have heard good things about blogengine.net.
    Is there a way I can transfer all my wordpress content into it?
    Any help would be really appreciated!

  7. Jacquelyn 2019年7月22日 下午4:29 回复

    I am sure this post has touched all the internet users, its really really fastidious post on building up
    new blog.

  8. Raleigh 2019年7月22日 上午5:15 回复

    Thank you for sharing your info. I truly appreciate your
    efforts and I will be waiting for your next post
    thank you once again.

  9. Marguerite 2019年7月22日 上午3:21 回复

    Hello to all, the contents present at this site are truly remarkable for people experience, well, keep up the good work fellows.

  10. Irwin 2019年7月22日 上午12:50 回复

    Spot on with this write-up, I really think this amazing site needs far more attention. I’ll probably
    be returning to see more, thanks for the information!

  11. Malinda 2019年7月21日 下午6:17 回复

    I think the admin of this site is genuinely working hard in favor of his site,
    because here every material is quality based material.

  12. Stephaine 2019年7月21日 下午1:18 回复

    I simply couldn’t depart your site before suggesting that
    I extremely loved the usual info a person supply to your guests?
    Is gonna be back regularly in order to check out new posts

  13. Diego 2019年7月20日 上午10:27 回复

    Your means of telling the whole thing in this article is genuinely pleasant, every one be capable of easily be aware of it, Thanks a
    lot.

  14. Nell 2019年7月19日 下午8:12 回复

    Attractive section of content. I just stumbled upon your website and in accession capital to assert
    that I acquire actually enjoyed account your blog posts.
    Anyway I’ll be subscribing to your augment and even I achievement you access consistently fast.

  15. Rudy 2019年7月18日 下午7:15 回复

    Hello I am so grateful I found your site, I really found you by accident, while I was researching on Askjeeve
    for something else, Anyhow I am here now and would just like to say thanks for a incredible post and a
    all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have saved it and also
    added your RSS feeds, so when I have time I will be back to read much more,
    Please do keep up the fantastic job.

  16. Olivia 2019年7月18日 下午6:17 回复

    Thanks for some other great post. Where else may just anyone
    get that kind of information in such a perfect approach of writing?
    I’ve a presentation next week, and I am on the look for such
    information.

  17. Del 2019年7月16日 下午11:54 回复

    It’s awesome in support of me to have a website,
    which is useful in favor of my knowledge. thanks admin

  18. Louis 2019年7月16日 上午3:48 回复

    Hello colleagues, how is everything, and what you
    desire to say on the topic of this post, in my view its in fact amazing designed for me.

  19. Seth 2019年7月13日 下午2:25 回复

    Thank you for some other informative blog. The place else may
    just I get that kind of information written in such a perfect manner?
    I have a mission that I am simply now running on, and I have been on the glance out for such info.

  20. Jewel 2019年7月13日 上午6:18 回复

    No matter if some one searches for his vital thing, thus he/she desires
    to be available that in detail, so that thing is maintained over
    here.

  21. Gerald 2019年7月11日 下午8:12 回复

    Touche. Great arguments. Keep up the great work.

  22. Hollis 2019年7月10日 上午1:08 回复

    Thanks for some other informative web site.

    The place else may I get that kind of info written in such an ideal way?
    I have a venture that I am simply now running on, and I have been at the look out for such information.

  23. bit.ly 2019年6月28日 下午12:42 回复

    Greetings! Very helpful advice in this particular article!
    It is the little changes which will make the largest changes.
    Many thanks for sharing!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Ɣ回顶部