如何使用Laravel和Vue.js打开list_parent_id

时间:2018-12-11 22:26:04

标签: vue.js laravel-5.7

我有一个带有下表的类别表。

Categories

我想单击parent_id => 0来打开parent_id => 1的折叠子类别。

Category.php

class Category extends Model {
  public function parent() {
    return $this->belongsTo(Category::class, 'parent_id');
  }

  public function children() {
    return $this->hasMany(Category::class, 'parent_id');
  }
}

Advert.vue

<template>
    <div>
        <h3>List of free ads</h3>
        <ul class="list-group">
            <li v-for="category in categories" :id="'select'+category.id " @click="sendAdvert(category.id)" class="list-group-item">{{ category.name }}</li>
        </ul>
    </div>
</template>

<script>
    export default {
        props: ['categories'],
        data() {
            return {
                adverts: []
            }
        },

        methods: {
            sendAdvert: function (id) {
                const self = this;

                axios.post('adverts/store',
                    { id: id }, {
                }).then((response) => {
                    this.adverts = response.data;
                }).catch((response) => {
                    self.error = 1;
                    console.log('Errors');
                });
            }
        }
    }
</script>

web.php

Route::resource('adverts', 'AdvertController');

AdvertController.php

public function index()
{
    $categories = Category::orderBy('id', 'desc')->get();
    return view('Home.adverts', ['categories' => $categories]);
    //$categories = Category::where('parent_id', 0)->get();
    //return view('Home.adverts', compact('categories'));
}

public function store(Request $request)
{
    $id = $request->id;
    $category = Category::find($id);

    return $category->children;
}

当我单击“汽车”并使用vuejs打开Collapse菜单时,我想看到汽车。

  • 魔鬼
  • 塔兹

当我单击“汽车”时,出现405错误。

Vuejs

0 个答案:

没有答案