如何使用Laravel和Vue JS在数据库中显示单个记录?

时间:2020-06-01 18:34:10

标签: javascript php laravel vue.js

HI如何使用vue js在数据库上显示一条记录?我设法显示多行,但无法显示单行。这就是我所做的。

=IF(AND(B2="Lodavan-Boleropickup",C2<"30"),CELL("color",D2),"")

这是我的api路径:(如果我转到'/ api / view / 1'),我成功获取了数据,我认为我的主要错误是在显示它。

<template>
  <div class="container">
    <div class="card-panel z-depth-5 grey lighten-2">
      <blockquote>
        <h3>
          <b>{{tutorial.attributes.title}}</b>
        </h3>
      </blockquote>
      <p class="flow-text">{{tutorial.attributes.content}}</p>
    </div>
  </div>
</template>

<script>
export default {
    name: 'tutorial',
  data: function() {
    return {
        tutorial = {
            id: '',
            content : null,
            title : '',
        },
    };
  },
  mounted() {
    const tutorial_id = this.id;
  },
  props: ['id'],
  methods: {
    get_tutorial: function() {
      //load the tutorials from the api
      axios
        .get('/api/view/' + tutorial_id )
        .then(response => {
          // asign it to data
          this.id = response.data.data.id;
          this.cpntent = response.data.data.content;

        })
        .catch(function(error) {
          // catch errors
          console.log(error);
        });
    }
  }
};
</script>

这是我的刀刃:

Route::get('/view/{id}','TutorialsController@view_tutorial');



 public function view_tutorial($id){ 
      return TutorialResource::collection(Post::all()->where('id',$id));
}

基本上,我的模板@extends('layouts.main') @section('styles') <style> .card-panel { border-radius:5px; } </style> @stop @section('content') <div class="app "> <view-tutorial :id="{{ $post->id }}" > </view-tutorial> </div> @endsection @section('scripts') <script> <script src="{{mix('js/app.js')}}"></script> </script> @ensection 中有一个href,当我单击它时,它会将我重定向到一个显示帖子内容的页面。这是我的:href="'/post/' + tutorial.id + '/' + tutorial.slug"网络路线 当我点击它时,它成功将我重定向到一个视图,在该视图中,我将使用vue js显示数据。在我的模板中,有一个道具会将ID发送到组件,并使用该ID进行一些axios获取请求,这样我就可以在数据库中获取特定的记录。我可以使用默认刀片语法显示它,但是为了学习在vue js上尝试使用它。很抱歉出现菜鸟问题。

这是我的错误: 在我的控制台中: 语法错误:C:\ projects \ php \ blogSite \ resources \ js \ components \ View_tutorial.vue:意外令牌(19:17)

在我的命令提示符下:

Route::get('/post/{id}/{slug}', 'PostController@read_post');

谢谢。我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用路由模型绑定。并从您的数据库中获取特定记录。

阅读文档: https://laravel.com/docs/7.x/routing#route-model-binding