■、创建迁移文件
php artisan make:migration creat_arical_table
php artisan make:migration creat_author_table
■、执行生成数据表的迁移文件
php artisan migrate
■创建模型
php artisan make:model /Home/Artical
■创建控制器
php artisan make:controller TestController
■、数据表回滚命令
php artisan migrate:rollback
php artisan migrate:rollback --step=2
■、填充器
php artisan make:seeder ArticalAndAuthorTableSeeder
php artisan db:seed --class=ArticalAndAuthorTableSeeder
===================================================
数据表外键的定义方法
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('author');
唯一的:
->unique();
可以为空:
->nullable();
主键:
->primary();
===================================================
■数据库包含方法
use Illuminate\Support\Facades\DB;
一对一
注意:在写关联模型的时候要分析出谁关联谁
当前例子主模型是文章模型
文章关联作者
需要将关联的代码写在主模型中
return $this->hasOne(Phone::class, 'foreign_key', 'local_key');
$data = $this->hasOne('\App\Models\Home\Author','id','author_id');
数据库填充
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use DB;
class ArticalAndAuthorTableSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//作者表
DB::table('author')->insert([
[
'author_name'=> '人民网'
],
[
'author_name'=> '百度'
],
[
'author_name'=> '谷歌'
],
]);
//文章表
DB::table('artical')->insert([
[
'author_id' => rand(1,3),
'arical_name' => '我有一朵小花'
],
[
'author_id' => rand(1,3),
'arical_name' => '我是小灰猪'
],
[
'author_id' => rand(1,3),
'arical_name' => '你好太阳'
],
]);
//评论表
DB::table('comment')->insert([
[
'comment'=> '写的很好,继续加油',
'artical_id' => rand(1,3)
],
[
'comment'=> '内容不明确,请修改',
'artical_id' => rand(1,3)
],
[
'comment'=> '错误的观点',
'artical_id' => rand(1,3)
],
[
'comment'=> '诗和远方',
'artical_id' => rand(1,3)
],
]);
}
}
主模型
<?php
namespace App\Models\Home;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Artical extends Model
{
use HasFactory;
protected $table = 'artical';
//关联模型操作
public function author(){
return $this->hasOne('App\Models\Home\Author','id','author_id');
}
}
关联模型
<?php
namespace App\Models\Home;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
use HasFactory;
protected $table = 'author';
}
控制器
public function test1()
{
$data = DB::table('artical')->select('artical.id','artical.arical_name','author.author_name')
->leftjoin('author','artical.author_id','=','author.id')->get();
// $data = DB::table(artical)->get();
dd($data);
//return $data;
}
public function test2()
{
//查询数据
$data = \App\Models\Home\Artical::get();
//dd($data);
//循环展示
foreach($data as $key =>$value){
echo $value ->id .' ' .$value->arical_name .' '.$value ->author->author_name .'<br/>';
}
}
路由
//一对一
Route::get('/test2','App\Http\Controllers\UserController@test2');
一对多
模型
<?php
namespace App\Models\Home;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Artical extends Model
{
use HasFactory;
protected $table = 'artical';
//关联模型操作
public function author(){
return $this->hasOne('App\Models\Home\Author','id','author_id');
}
public function comment(){
return $this->hasMany('App\Models\Home\Comment','artical_id','id');
}
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function test1()
{
$data = DB::table('artical')->select('artical.id','artical.arical_name','author.author_name')
->leftjoin('author','artical.author_id','=','author.id')->get();
// $data = DB::table(artical)->get();
dd($data);
//return $data;
}
public function test2()
{
//查询数据
$data = \App\Models\Home\Artical::get();
//dd($data);
//循环展示
foreach($data as $key =>$value){
echo $value ->id .' ' .$value->arical_name .' '.$value ->author->author_name .'<br/>';
}
}
public function test3()
{
$data = \App\Models\Home\Artical::get();
foreach($data as $key =>$value){
echo $value ->id. ' '. $value -> arical_name .' '.$value->author->author_name.' ';
foreach($value ->comment as $key1=>$value1)
echo $value1 ->comment. '<br/>';
}
}
}
多对多
多对多需要有关系表,但是关系表不需要创建模型。
不需要给关系表创建模型
模型
<?php
namespace App\Models\Home;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Artical extends Model
{
use HasFactory;
protected $table = 'artical';
//关联模型操作
public function author(){
return $this->hasOne('App\Models\Home\Author','id','author_id');
}
public function comment(){
return $this->hasMany('App\Models\Home\Comment','artical_id','id');
}
public function keyword(){
return $this->belongsToMany('App\Models\Home\Keyword','artical_keyword','artical_id','keyword_id');
}
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function test1()
{
$data = DB::table('artical')->select('artical.id','artical.arical_name','author.author_name')
->leftjoin('author','artical.author_id','=','author.id')->get();
// $data = DB::table(artical)->get();
dd($data);
//return $data;
}
public function test2()
{
//查询数据
$data = \App\Models\Home\Artical::get();
//dd($data);
//循环展示
foreach($data as $key =>$value){
echo $value ->id .' ' .$value->arical_name .' '.$value ->author->author_name .'<br/>';
}
}
public function test3()
{
$data = \App\Models\Home\Artical::get();
foreach($data as $key =>$value){
echo $value ->id. ' '. $value -> arical_name .' '.$value->author->author_name.' ';
foreach($value ->comment as $key1=>$value1)
echo $value1 ->comment. '<br/>';
}
}
public function test4()
{
$data = \App\Models\Home\Artical::get();
foreach($data as $key =>$value){
echo "当前文章的名称为:". $value ->id. ' '. $value -> arical_name .' '.$value->author->author_name.' '. "当前文章的关键词为:".'<br/>';
foreach($value ->keyword as $key1=>$value1)
echo $value1 ->keyword. '<br/>';
}
}
}