聯表要求至少得有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的路上,若是你以爲本文對你有所幫助的話,那就請關注點贊評論三連吧,謝謝,你的確定是我寫博的另外一個支持。