使用 eloquent 和 vuejs 时如何检索数据库列

Posted

技术标签:

【中文标题】使用 eloquent 和 vuejs 时如何检索数据库列【英文标题】:how to retrieve database columns when using eloquent with vuejs 【发布时间】:2018-08-09 11:03:14 【问题描述】:

我正在尝试使用 AXios get request 从数据库中获取数据。我有 2 个模型(内容和 Word)具有多对多关系。在我的控制器中我正在尝试这个:

public function fetchCourses()
    $dayOne = Content::where(['course_id'=>'1','day_id'=>'1'])->first();
    return $dayOne;

在 vue 中我正在尝试这样:

axios.get('/getCourse').then((response)=>
    //console.log(response.data);
    this.content = response.data;
) 

然后在组件中:

<iframe   :src="content.video_clip"  frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>

video_clip 是我的 Contents 表中的列名之一。

现在我不仅想从 Word Model 中获取内容,还想获取单词。我怎样才能做到这一点?

【问题讨论】:

您还在寻找答案吗? 是的。会很高兴 【参考方案1】:

您必须加载words 关系(假设是名称):

$dayOne = Content::where(['course_id'=>'1','day_id'=>'1'])->with('words')->first();

然后你可以在你的前端访问单词:

this.content.words

【讨论】:

以上是关于使用 eloquent 和 vuejs 时如何检索数据库列的主要内容,如果未能解决你的问题,请参考以下文章