联表要求至少得有2张表(除了本身【表】链接本身【表】,自联查询),而且仍是存在关系的两张表。 例如:能够创建2张表:文章表、做者表。php
文章表(article):sql
id | 主键 |
---|---|
Article_name | 文章名称,varchar(50),not null |
Author_id | 做者id,int,not null |
做者表(author):markdown
id | 主键 |
---|---|
Author_name | 做者名称 ,varchar(20),not null |
php artisan make:migration create_article_table
php artisan make:migration create_author_table
article
加上字段:学习
$table->increments('id');
$table->string('article_name', 50)->comment('文章标题');
$table->integer('author_id') -> comment('做者id');
$table->timestamps();
复制代码
author
加上字段:spa
$table->increments('id');
$table->string('author_name', 20) -> comment('做者名称');
$table->timestamps();
复制代码
而后执行迁移文件: php artisan migrate
code
一、建立填充器文件(能够将多个数据表的写入操做写在一块儿) php artisan make:seeder ArticleAndAuthorTableSeeder
二、编写数据模拟的代码
三、执行填充器
php artisan db:seed --class=ArticleAndAuthorTableSeeder
orm
要求查询数据表(文章表、做者表),查询出文章的信息包含了做者名称,联表查询一共有:内联表(inner)、左联表(left)、右联表(right)。图片
原始sql语句:【左外联表】
select article.id,article.article_name, author.author_name from article left join author on article.author_id = author.id
路由
将上述的sql语句改为链式操做:rem
语法:DB 门面/模型 -> join 联表方式名称(关联的表名,表1的字段,运算符,表2的字段)
左链接:若是你是想要执行左连接
而不是内连接
可使用leftJoin
方法,该方法和join
方法的用法 建立路由: 建立方法:
效果:
在学习的php的路上,若是你以为本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的确定是我写博的另外一个支持。