Giter VIP home page Giter VIP logo

laravel-eloquent-lazy-loading's Introduction

安装

环境文件

在项目的根目录中附带一个.env.example文件,请将其拷贝重命名为.env。可以使用如下命令完成操作cp .env.example .env

注意:确保您的系统上显示隐藏的文件。

Composer

laravel项目依赖通过[composer](http://getcomposer.org/)工具进行管理。

第一步是通过在终端中进入到项目中并输入以下命令来安装依赖

composer install

创建数据库

在服务器上创建数据库,然后更新项目根目录下的.env文件上如下的相关行:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

更新上面的行已完成数据库相关设置。

项目使用sqlite作为数据库存储,使用命令touch database/database.sqlite,生成sqlite数据库文件。

并将数据库连接信息修改为

DB_CONNECTION=sqlite

artisan命令

我们要做的第一件事就是设置 Laravel 在进行加密时使用的密钥。

php artisan key:generate

应该看到一条绿色消息,指出您的密钥已成功生成,以及你应该看到.env文件中的app_key变量反映出来。

现在是时候查看数据库连接信息是否正确。运行内置的迁移来创建数据库表

php artisan migrate

如果没有看到错误,应该会看到每个迁移表的消息,而不是数据库连接信息很可能不正确。

运行数据库迁移文件以添加一些默认数据

php artisan db:seed

应该得到每个迁移文件的消息,并且应该可以看到数据库表中的信息。

tinker 命令

模型预加载

运行php artisan think命令后,在tinker终端中执行下面的代码:

namespace App;

$posts = Post::with('author')->get();
$posts->map(function ($post) {
    return $post->author;
});

模型嵌套预加载

运行php artisan think命令后,在tinker终端中执行下面的代码:

namespace App;

$posts = Post::with('author.profile')->get();
$posts->map(function ($post) {
    return $post->author->profile;
});

模型惰性预加载

namespace App;

$posts = Post::all();
$posts->load('author.profile');
$posts->first()->author->profile;

laravel-eloquent-lazy-loading's People

Contributors

curder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.