我有一个带有下表的类别表。
我想单击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错误。