8. 使用Elequent创建表与表之间的关系

从零开始学Laravel目录php

Eloquent提供了一个易于阅读且很形象化的表和表之间的关系对应和调用,好比说,一条评论是属于一个帖子的,一个帖子拥有不少的评论,一篇帖子和一个视频页同时拥有不少标签,下面咱们来看看如何建立这些关系。html

咱们就以一篇帖子有不少的评论来举列,帖子和评论是一对多的关系,咱们上一节已经创建了帖子的表posts, 下面咱们来创建评论表comments和评论的ModelComment, 在上一节咱们是经过下面两条命令来创建migration文件和Model的laravel

// 创建帖子的migration文件
php artisan make:migration create_posts_table --create=posts

// 创建帖子Model
php  artisan make:model Post

咱们在创建评论表和模型的时候,用另外一种方法,咱们在创建Model的时候,只要加上-m参数,就能在创建Model的时候,同时生成migration文件了。(执行php artisan命令都是须要进入到项目的根目录下执行的,之后我就不说这点了)sql

➜ php artisan make:model Comment -m                           
Model created successfully.
Created Migration: 2016_11_14_125930_create_comments_table

从上面咱们能够看出,咱们建立模型的时候,laravel也帮咱们生成了表名为模型名复数的migration文件,咱们打开这个migration文件,并更改up()函数以下:数据库

public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id')->unsigned()->index();
            $table->text('content');
            $table->timestamps();
        });
    }

上面的post_id是posts表的外键,在正式开发的时候,咱们须要作到外键约束,同时作到删除的及联操做,这里咱们先不添加了。咱们将这个表执行到数据库中数组

➜ php artisan migrate              
Migrated: 2016_11_14_125930_create_comments_table

如今在咱们的app目录下,咱们已经有了Post.php和Comment.php两个模型,下面咱们打开tinker缓存

➜ php artisan tinker 
Psy Shell v0.7.2 (PHP 7.0.12 — cli) by Justin Hileman
>>>

如下的代码都在tinker中执行生成,咱们先来获取第一条帖子数据:bash

>>> $post = App\Post::first();
=> App\Post {#636
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }

咱们再来建立属于帖子1的一条评论,咱们此次建立先手动的来维护外键(post_id):app

>>> $comment = new App\Comment;
=> App\Comment {#625}

>>> $comment->content = 'Some comment for the post';
=> "Some comment for the post"

>>> $comment->post_id = 1;
=> 1

>>> $comment->save();
=> true

>>> App\Comment::all();
=> Illuminate\Database\Eloquent\Collection {#640
     all: [
       App\Comment {#641
         id: "1",
         post_id: "1",
         content: "Some comment for the post",
         created_at: "2016-11-15 01:07:53",
         updated_at: "2016-11-15 01:07:53",
       },
     ],
   }
>>>

下面咱们来经过Eloquent在各模型间创建表与表之间的对应关系,首先咱们先理一下,一个帖子会有不少评论,A post has many comments, 一个评论属于一个帖子:a comment that belongs to a post,咱们打开Post.php,编写一个comments()函数,意思是一个帖子有不少评论,因此注意这个comments()必定要写成复数形式,写代码单词的单复数对于易读性来讲很是的重要。编辑器

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

好的,咱们再进入到tinker中来测试下:
咱们先拿到第一个帖子:

>>> $post = App\Post::first();
=> App\Post {#636
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }

咱们拿取这个帖子的全部评论,咱们能够这么写$post->comments()->get(),也能够这么写$post->comments;, 后面这种写法,laravel文档叫它动态属性。

>>> $post->comments;
=> Illuminate\Database\Eloquent\Collection {#633
     all: [
       App\Comment {#637
         id: "1",
         post_id: "1",
         content: "Some comment for the post",
         created_at: "2016-11-15 01:07:53",
         updated_at: "2016-11-15 01:07:53",
       },
     ],
   }

用上面的方法拿出的数据实际上是Comment对象的一个集合(Collection),咱们能够像操做数组同样的操做这个集合,如:

>>> $post->comments[0];
=> App\Comment {#637
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
   }

固然,开发的时候不多像上面这么作,由于Laravel给咱们提供了不少关于操做这个集合的方法,好比说,取集合中的第一个对象:

>>> $post->comments->first();
=> App\Comment {#637
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
    }

这里有一个很是重要的地方,咱们来尝试下面这条语句:

>>> $post->comments()->first();
=> App\Comment {#651
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
   }

咱们看到$post->comments->first();$post->comments()->first();这两条语句输出的结果是同样的,可是具体的操做却不一样,咱们假设帖子1有500条评论,那么$post->comments->first();会先经过$post->comments从数据库拿到这500条评论的数据放进集合,而后再从集合中获取第一条数据。而$post->comments()->first();呢,当执行到$post->comments()时,它并无拿出这500条数据,这里还处于一个查询的阶段,等到执行first()时,从数据库只拿出一条数据,咱们应该使用哪一种写法,你们应该就很明白了。曾有人说不要用ORM,太慢,可是不少慢的缘由不在于ORM, 而是不了解它,没用好而已。

咱们再看看这两条语句执行的原生SQL语句,咱们在tinker中让每次执行语句的时候都打印出原生的SQL,能够这么作:

➜ php artisan tinker
Psy Shell v0.7.2 (PHP 7.0.12 — cli) by Justin Hileman
>>> DB::listen(function ($query) { var_dump($query->sql); });
=> null

咱们再来拿第一个帖子:

>>> $post = App\Post::first();
string(29) "select * from "posts" limit 1"
=> App\Post {#637
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }
>>>

拿帖子的全部评论,本身看下sql语句:

>>> $post->comments;
string(92) "select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null"
=> Illuminate\Database\Eloquent\Collection {#623
     all: [
       App\Comment {#638
         id: "1",
         post_id: "1",
         content: "Some comment for the post",
         created_at: "2016-11-15 01:07:53",
         updated_at: "2016-11-15 01:07:53",
       },

咱们再来执行一次$post->comments;

>>> $post->comments;
=> Illuminate\Database\Eloquent\Collection {#623
     all: [
       App\Comment {#638
         id: "1",
         post_id: "1",
         content: "Some comment for the post",
         created_at: "2016-11-15 01:07:53",
         updated_at: "2016-11-15 01:07:53",
       },
     ],
   }

咱们发现此次没有出现SQL语句,那是由于laravel已经缓存了此次查询的结果,咱们再来看下$post的结果,它也被缓存了,而且咱们查询的$post->comments的内容也被插入到这个对象中。

>>> $post
=> App\Post {#637
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
     comments: Illuminate\Database\Eloquent\Collection {#623
       all: [
         App\Comment {#638
           id: "1",
           post_id: "1",
           content: "Some comment for the post",
           created_at: "2016-11-15 01:07:53",
           updated_at: "2016-11-15 01:07:53",
         },
       ],
     },
   }

若是咱们刷新获取下$post,在打印$post, 你们在看下结果:

>>> $post = $post->fresh();
string(44) "select * from "posts" where "id" = ? limit 1"
=> App\Post {#643
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }

>>> $post
=> App\Post {#643
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }

由于laravel会缓存查询,因此你们在测试的时候必定要加上fresh()才能准确,咱们来看$post->comments->first(),执行的时候要加上fresh(),这很是的重要,千万不要作了错误的测试误导了你。

>>> $post->fresh()->comments->first();
string(44) "select * from "posts" where "id" = ? limit 1"
string(92) "select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null"
=> App\Comment {#630
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
   }

看上面的第二条SQL语句,它是查询出数据库的全部的评论。

咱们在来看$post->comments()->first()这条语句:

>>> $post->fresh()->comments()->first();
string(44) "select * from "posts" where "id" = ? limit 1"
string(100) "select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null limit 1"
=> App\Comment {#644
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
   }

咱们看第2条SQL语句,用这种写法,只会从数据库拿出1条记录,这里我说这么多,是由于我看见不少人在滥用动态属性,因此咱们必定要注意这点。

好了,咱们如今看看在Comment模型中如何写对应的关系呢?拿出以前咱们写的英文句子:a comment that belongs to a post, 咱们打开Comment.php,

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    // 注意这里post应该是单数形式
    public function post()
    {
       return $this->belongsTo('App\Post');
        
        // 若是你使用的是PhpStrom编辑器,你也能够按下面这么写,这样点击能够跳转到对应的类文件中
        // return $this->belongsTo(Post::class);
    }
}

咱们从新打开tinker, 当你修改了代码后,要从新打开tinker再测试,不然tinker执行的仍是修改前的代码:

>>> $comment = App\Comment::first();
=> App\Comment {#636
     id: "1",
     post_id: "1",
     content: "Some comment for the post",
     created_at: "2016-11-15 01:07:53",
     updated_at: "2016-11-15 01:07:53",
   }
>>> $comment->post;
=> App\Post {#637
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }
>>>

下面咱们来看下,咱们建立一条评论的时候,如何让Eloquent的关联关系给咱们自动维护外键:
咱们先建立一个$comment对象,设置它的内容:

>>> $comment = new App\Comment;
=> App\Comment {#622}
>>> $comment->content = 'Here is another comment.';
=> "Here is another comment."
>>>

如今咱们不用手动去设置post_id,咱们直接找到评论须要属于的post,好比,仍是打算让这条评论属于第一个帖子:

>>> $post = App\Post::first();
=> App\Post {#639
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }

下面咱们只须要经过Post的Comments()关联去存储属于它的评论便可,会自动设置$post对象的ID到对应的评论的post_id

>>> $post->comments()->save($comment);
=> App\Comment {#622
     content: "Here is another comment.",
     post_id: 1,
     updated_at: "2016-11-15 02:38:01",
     created_at: "2016-11-15 02:38:01",
     id: 2,
   }

如今经过$post->comments;查看下,发现已经存在两条评论了。

好了,上面的代码都是在tinker中测试的,咱们如今进入了PostsController中,修改下show()函数:

public function show(Post $post)
    {        
        return view('posts.show', compact('post'));
    }

而后创建show.blade.php视图层,输入如下代码:

@extends('layout')

@section('content')
    <h1>{{ $post->title }}</h1>
    <ul>
        @foreach ($post->comments as $comment)
            <li>{{ $comment->content }}</li>
        @endforeach
    </ul>
@stop

好,咱们访问下: http://localhost:8000/posts/1
表的关联关系

咱们刚才是用save()方法来存储一条评论,如今咱们来试试使用create()方法来建立呢! 仍是打开tinker

fillable

嗯, 出现了匹配异常错位,这是Laravel对使用create()update()这两个函数作的保护机制,咱们知道create()update()能够批量的设置表字段,若是不作一些保护错位的话,可能会被人经过设置某些字段的值来串改你的数据,因此在Laravel中,你容许批量建立和修改的字段,你都要本身在模型中明确指定,咱们打开Comment.php, 为Comment模型添加$fillbale属性:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    // 容许使用create()和update()批量建立和更新的字段

    protected $fillable = ['content'];
    
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

下面从新启动tinker,在执行一次,就能成功建立了

Psy Shell v0.7.2 (PHP 7.0.12 — cli) by Justin Hileman
>>> $post = App\Post::first();
=> App\Post {#636
     id: "1",
     title: "My New Post Title",
     content: "new post content",
     created_at: "2016-11-14 07:22:32",
     updated_at: "2016-11-14 07:22:32",
   }
>>> $post->comments()->create(['content' => 'Yet another comment about this post']);
=> App\Comment {#640
     content: "Yet another comment about this post",
     post_id: 1,
     updated_at: "2016-11-15 03:08:25",
     created_at: "2016-11-15 03:08:25",
     id: 3,
   }
>>>

咱们在进入到posts/index.balde.php中,咱们给帖子都加上连接:

@extends('layout')

@section('content')
    <h1>全部的帖子</h1>

    @foreach ($posts as $post)
        <h2><a href="posts/{{ $post->id }}">{{ $post->title }}</a></h2>
        <p>{{ $post->content }}</p>
    @endforeach
@stop

加连接有不少方法,也有人会写成一个函数,如<a href="{{ $post->path() }}">, 而后在Post模型层写一个path()函数

public function path()
{
    return '/posts/' . $this->id;
}

不过把这个写成函数我认为也没多大必要。好了,本节到这里结束。

Eloquent关联模型的用法你们应该知道了,可是除了一对多关系,还有一对一,多对多,多态一对多等,要用的时候,能够去查这篇文档

相关文章
相关标签/搜索