[英]How to combine 3 tables with eloquent in Laravel
How to combine 3 tables with eloquent inaravel?如何将 3 个表与 eloquent inaravel 结合起来?
Hi Whomever help me, I need your help.嗨,谁帮助我,我需要你的帮助。
How do you combine the following three tables with eloquent in laravel? laravel中的eloquent如何结合以下三个表?
The following table structure and figures:下表结构和数字:
Tag table structure {tag_id: primary key, tag_name, tag_category}标签表结构 {tag_id: 主键, tag_name, tag_category}
Article table structure {article_id: primary key, date_posting, content, tag_id: foreign key}文章表结构{article_id:主键,date_posting,内容,tag_id:外键}
Review table structure {review_id: primary key, date_review, reviewer_name, review_content, article_id: foreign key}审阅表结构{review_id:主键,date_review,reviewer_name,review_content,article_id:外键}
Can you give me sample source code for view.blade, controller and model.你能给我 view.blade、controller 和 model 的示例源代码吗?
Thank you very helpful answer:)谢谢你非常有帮助的答案:)
From your image从你的形象
Tag are are having articles and articles are having reveiw标签有文章,文章有评论
Now Tag shares a one-to-many andmany-to-one(inverse) relationship with Article现在 Tag 与 Article 共享一对多和多对一(反向)关系
use App\Article;
class Tag extends Model{
public function articles()
{
return $this->hasMany(Article::class);
}
}
Article model文章 model
use App\Tag;
use App\Review;
class Article extends Model{
//Inverse relation for Tag and Article
public function tag()
{
return $this->belongsTo(Tag::class);
}
//Articles having reviews
public function reviews()
{
return $this->hasMany(Review::class);
}
}
In Review model审查中 model
use App\Article;
class Review extends Model {
public function article()
{
return $this->belongsTo(Article::clas);
}
}
Now you can retrieve all models data using eager loading现在您可以使用预加载检索所有模型数据
App\Tag::with('articles.reviews')->get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.