legend3---lavarel多对多模型操作实例

时间:2022-01-17 20:01:20

legend3---lavarel多对多模型操作实例

一、总结

一句话总结:

在多对多模型中,增加关系表的数据 需要 弄一个和关系表一对多的模型关系

1、在lavarel关系模型中,课程和标签表是多对多,那么我如何增加课程标签联合表的数据?

定义一个一对多的模型,因为课程和标签表是多对多,那么课程和课程标签联合表是一对多
//与课程标签联合表的一对多关系
public function hasManyLessonTags(){
return $this->hasMany(\App\Model\Admin\TagAndLesson::class,'tl_l_id','l_id');
}
//2、插入标签课程数据--关系表
$tags=$request['tags'];
foreach ($tags as $tag){
$tag_lesson=[];
$tag_lesson['tl_t_id']=$tag;
//$tag_lesson['tl_l_id']=$lesson['l_id'];
//TagAndLesson::create($tag_lesson);
//dd($tag_lesson);
$lesson->hasManyLessonTags()->create($tag_lesson); }

2、在lavarel关系模型中,定义模型关系的第一个参数是什么意思:例如$this->hasMany(Video::class,'v_l_id','l_id');?

关联的模型,也就是【最终要获取数据的表模型】,这里是哪个模型就表示要操作哪个表

3、在lavarel关系模型中,课程和标签表是多对多,我在定义好了多对多的模型关系,我能否用这个模型关系增加课程标签联合表的数据?

不能,因为模型关系的第一个参数是最终要操作数据的表模型

二、lavarel多对多模型操作实例

1、关系和数据表结构

课程和标签表是多对多

课程表:lessons

CREATE TABLE `lessons`  (
`l_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`l_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`l_introduce` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`l_preview` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`l_is_commend` tinyint(4) NOT NULL,
`l_is_hot` tinyint(4) NOT NULL,
`l_click` int(11) NOT NULL DEFAULT 0 COMMENT '点击数/浏览数',
`l_like` int(11) NOT NULL DEFAULT 0 COMMENT '点赞数',
`l_comment_num` int(11) NOT NULL DEFAULT 0 COMMENT '评论数',
`created_at` timestamp(0) NULL DEFAULT NULL,
`updated_at` timestamp(0) NULL DEFAULT NULL,
PRIMARY KEY (`l_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

标签表:tags

CREATE TABLE `tags`  (
`t_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`t_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`created_at` timestamp(0) NULL DEFAULT NULL,
`updated_at` timestamp(0) NULL DEFAULT NULL,
PRIMARY KEY (`t_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

课程标签表:tag_and_lessons

CREATE TABLE `tag_and_lessons`  (
`tl_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`tl_t_id` int(11) NOT NULL COMMENT '标签id',
`tl_l_id` int(11) NOT NULL COMMENT '课程id',
`created_at` timestamp(0) NULL DEFAULT NULL,
`updated_at` timestamp(0) NULL DEFAULT NULL,
PRIMARY KEY (`tl_id`) USING BTREE,
INDEX `tag_and_lessons_tl_t_id_index`(`tl_t_id`) USING BTREE,
INDEX `tag_and_lessons_tl_l_id_index`(`tl_l_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

2、模型代码

app/Model/Lesson.php

 <?php

 namespace App\Model;

 use Illuminate\Database\Eloquent\Model;

 class Lesson extends Model
{
protected $primaryKey='l_id';
/**
* 与视频表模型的一对多关联
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function videos(){
return $this->hasMany(Video::class,'v_l_id','l_id');
} //与标签的多对多关系
public function hasManyTags(){
return $this->belongsToMany('App\Model\Tag','tag_and_lessons','tl_l_id','tl_t_id');
} //与课程标签联合表的一对多关系
public function hasManyLessonTags(){
return $this->hasMany(\App\Model\Admin\TagAndLesson::class,'tl_l_id','l_id');
}
}

app/Model/Tag.php

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
//$guarded表示不允许批量填充的字段
protected $primaryKey='t_id';
protected $guarded=[]; }

app/Model/Admin/TagAndLesson.php

<?php

namespace App\Model\Admin;

use Illuminate\Database\Eloquent\Model;

class TagAndLesson extends Model
{
//
protected $primaryKey='tl_id';
protected $guarded=[];
}

3、控制器中使用的代码

app/Http/Controllers/Admin/LessonController.php

     public function store(Request $request,Lesson $lesson)
{
DB::transaction(function () use ($request,$lesson) {
//dd($request->toArray());
$lesson['l_title']=$request['l_title'];
$lesson['l_introduce']=$request['l_introduce'];
$lesson['l_preview']=$request['l_preview'];
$lesson['l_is_commend']=$request['l_is_commend'];
$lesson['l_is_hot']=$request['l_is_hot'];
$lesson['l_click']=$request['l_click'];
$lesson->save();
//dd($lesson['l_id']);
//dd($lesson->toArray()); //2、插入标签课程数据--关系表
$tags=$request['tags'];
foreach ($tags as $tag){
$tag_lesson=[];
$tag_lesson['tl_t_id']=$tag;
//$tag_lesson['tl_l_id']=$lesson['l_id'];
//TagAndLesson::create($tag_lesson);
//dd($tag_lesson);
$lesson->hasManyLessonTags()->create($tag_lesson); } //videos数据过来的时候是json数据,true表示转成数组而非对象
$videos=json_decode($request['videos'],true);
//dd($videos);
foreach ($videos as $video){
unset($video['v_id']);
$lesson->videos()->create($video);
// $lesson->videos()->create([
// 'title'=>$video['title'],
// 'path'=>$video['path'],
// ]);
}
}); return redirect('/admin/lesson'); }